Spring的新注解
1 @Configuration
1.1 作用
- 用于指定当前类是一个Spring的配置类,当创建容器的时候会从该类上加载注解。获取容器的时候需要使用AnnotationApplicationContext(有@Configuration注解的类的.class)。
1.2 属性
- value:用于指定配置类的字节码。
1.3 应用示例
- 示例:
package com.sunxiaping.spring5.config; import org.springframework.context.annotation.Configuration; /**
* @Configuration 注解标注的类就代表当前类是一个配置类
*/
@Configuration
public class SpringConfiguration {
}
2 @ComponentScan
2.1 作用
- 用于指定Spring在初始化容器时要扫描的包。作用和在Spring的xml配置文件中的<context-component-scan base-package="com.sunxiaping"/>是一样的。
2.2 属性
- basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。
2.3 应用示例
- 示例:
package com.sunxiaping.spring5.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.sunxiaping.spring5")
public class SpringConfiguration {
}
3 @Bean
3.1 作用
- 该注解只能写在方法上,表明使用此方法创建一个对象,并且放入到Spring容器中。默认情况下,方法名就是Bean的id。
3.2 属性
- name:给当前@Bean注解方法创建的对象指定一个名称(即Bean的id)。
3.3 应用示例
- 示例:
package com.sunxiaping.spring5.config; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource;
import java.beans.PropertyVetoException; @Configuration
@ComponentScan("com.sunxiaping.spring5")
public class SpringConfiguration { @Bean
public DataSource dataSource() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true");
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setUser("root");
dataSource.setPassword("123456");
return dataSource;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
} @Bean
public QueryRunner queryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
} }
4 @PropertySource
4.1 作用
- 用于加载.properties文件中的配置。例如我们配置数据源的时候,可以把连接信息写到properties文件中,就可以使用此注解指定properties配置文件的位置。
4.2 属性
- value[]:用于指定properties文件位置。如果是在类路径下,需要写上classpath:
4.3 应用示例
- 示例:
- jdbc.properties
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
jdbc.user=root
jdbc.password=123456
- JdbcConfig.java
package com.sunxiaping.spring5.config; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource; import javax.sql.DataSource;
import java.beans.PropertyVetoException; @PropertySource("classpath:jdbc.properties")
public class JdbcConfig { @Value("${jdbc.driverClass}")
private String driverClass;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password; @Bean
public DataSource dataSource() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setDriverClass(driverClass);
dataSource.setUser(user);
dataSource.setPassword(password);
return dataSource;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
} @Bean
public QueryRunner queryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
}
5 @Import
5.1 作用
- 导入其他的配置类,在引起其他配置的时候,可以不用再写@Configuration注解。当然,写上也没什么关系。
5.2 属性
- value[]:用于指定其他配置类的字节码
5.3 应用示例
- 示例:
- JdbcConfig.java
package com.sunxiaping.spring5.config; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource; import javax.sql.DataSource;
import java.beans.PropertyVetoException; @PropertySource("classpath:jdbc.properties")
public class JdbcConfig { @Value("${jdbc.driverClass}")
private String driverClass;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password; @Bean
public DataSource dataSource() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setDriverClass(driverClass);
dataSource.setUser(user);
dataSource.setPassword(password);
return dataSource;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
} @Bean
public QueryRunner queryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
}
- SpringConfig.java
package com.sunxiaping.spring5.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@ComponentScan("com.sunxiaping.spring5")
@Import(JdbcConfig.class)
public class SpringConfiguration { }
6 通过注解获取容器
- 示例:
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
Spring的新注解的更多相关文章
- 阶段3 2.Spring_06.Spring的新注解_8 spring整合junit完成
Junit的核心Runner在执行的时候不会创建容器.同时它字节码文件,也改不了 spring整合junit 想办法把junit里面的不能加载容器的main方法换掉.从而实现创建容器.有了容器就可以实 ...
- 阶段3 2.Spring_06.Spring的新注解_5 spring的新注解-PropertySource
数据库的链接 次数是写死的 新建配置文件 定义成员变量 value注解实现 与配置文件的key对应 PropertySource 要想让spring去读取这个配置文件 resource编译后都跑到了. ...
- 阶段3 2.Spring_06.Spring的新注解_2 spring的新注解-Bean
下面要解决第二部分的配置问题 这两行一出场,就表示可以通过调用构造函数实例化.因为这都是newInstance 上面的需要加上参数,下面的没有任何参数 下面这俩实现的效果不一样. 下面这个除了会创建对 ...
- 阶段3 2.Spring_06.Spring的新注解_6 Qualifier注解的另一种用法
复制上面的数据源到下面改改名字 现在就是有两个数据源 创建一个eesy02的数据库 找到sql语句再创建Account表 现在就相当于有连个库一个eesy一个是eesy02这连个库. account里 ...
- 阶段3 2.Spring_06.Spring的新注解_4 spring的新注解-Import
把Configuration的直接先注释掉 那么运行测试类的查询所有 并不影响我们的使用 不写同样可以执行的原因是因为这里把SpringConfiguration这个类作为方法传入进去了 新建 Spr ...
- 阶段3 2.Spring_06.Spring的新注解_3 AnnotationConfigApplicationContext的使用
目前这个配置文件除了导约束就没有其他的内容了. 删除这个bean.xml文件 但是测试类里面还是读取的xml的信息 注解 查看ApplicationContext的 关系图 查看实现类的实现类 之前我 ...
- 阶段3 2.Spring_06.Spring的新注解_1 spring的新注解-Configuration和ComponentScan
解决测试类重复代码的问题,xml还是存在的问题,没法脱离xml文件 要想在QueryRunner上加注解,是加不了的 创建工程 复制依赖项到pom.xml 复制注解的工程里面的com文件夹 配置文件b ...
- 阶段3 2.Spring_06.Spring的新注解_7 spring整合junit问题分析
测试类重复代码的问题 这是之前的方式 运行findAll的方法,没有问题 测试人员不需要关心上面的方法,.应该关心的各个方法是否能够正常的运行 对于一个测试工程师,只要写完变量就可以测试了. 可以使用 ...
- Spring中基于注解的IOC(二):案例与总结
2.Spring的IOC案例 创建maven项目 导入依赖 pom.xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...
随机推荐
- IPython4_Notebook
目录 目录 前言 系统软件 Setup IPython Setup IPython Setup Notebook 临时指定镜像源 Install pyreadline Install pyzmq In ...
- python学习笔记:(十一)模块
模块是指一个包含定义的函数和变量的文件,其后缀名为.py.模块可以被别的程序引用,并使用其中的函数等功能. 1.import语句 如果需要使用模块,只需要在新模块中导入模块.使用import关键字 如 ...
- 微信小程序组件篇实战
实现效果如下: 实现代码如下: index.wxml: <!--index.wxml--> <view class="container"> <vie ...
- linux判断httpd端口是否打开
判断端口是否打开 lsof -i:80 判断端口打开了几个 lsof -i:80 | wc -l
- What is an Activation object in JavaScript ?
********************* from Professional JavaScript for Web Development Execution Context And Scope T ...
- Android 消息传递机制
线程间消息传递机制 1.消息怎么发送的? 我们都知道当调用Handler发送消息的时候,不管是调用 sendMessage,sendEmptyMessage,sendMessageDelayed还是其 ...
- Microsoft Remote Desktop for Mac
因为teamviewer 又限制经常断线,所以改用 Microsoft Remote Desktop 代替,用来从mac连接远程windows 主要记录一下下载地址,因为在mac app store ...
- 【HANA系列】SAP HANA跟我学HANA系列之创建分析视图一
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA跟我学HANA系 ...
- Centos6.5修改mysql登陆用户密码
1.修改mysql的登陆设置: vim /etc/my.cnf 并在[mysqld] 下面添加一句:skip-grant-tables=1 添加成功后保存退出. 2.重启mysql并修改密码 重启my ...
- 红帽学习笔记[RHCE]OpenLDAP 服务端与客户端配置
目录 OpenLDAP 服务端与客户端配置 关于LDIF 一个LDIF基本结构一个条目 属性 Object的类型 服务端 安装 生成证书 生成默认数据 修改基本的配置 导入基础数据 关于ldif的格式 ...