@Configuration
作用:用于指定当前类是一个配置类,当创建容器时会从该类上加载注解。相当于bean.xml文件。
属性:value用于指定配置类的字节码
//示例代码:
@Configuration//添加该注解后该类就是spring的配置类,相当于bean.xml
public class SpringConfiguration {
}
@ComponentScan
作用:用于指定 spring 在初始化容器时要扫描的包。作用和在 spring 的 xml 配置文件中的:
<context:component-scan base-package="com.itheima"/>是一样的。
属性:basePackages用于指定要扫描的包,和@Configuration注解中的 value 属性作用一样。
//示例代码:
@Configuration
@ComponentScan("com.zy.account")//指定要扫描的包
public class SpringConfiguration {
}
@Bean
作用:该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器。
属性:
name:给当前@Bean 注解方法创建的对象指定一个名称(即 bean 的 id)。如果不写默认是方法名称。
//示例代码:
public class JdbcConfig {
@Bean(name = "dataSources")
public DataSource createDataSources(){
try {
ComboPooledDataSource dataSources = new ComboPooledDataSource();
dataSources.setDriverClass("com.mysql.jdbc.Driver");
dataSources.setJdbcUrl("jdbc:mysql://localhost:3306/stu");
dataSources.setUser("root");
dataSources.setPassword("123456");
return dataSources;
}catch (Exception e){
throw new RuntimeException();
}
}
}
@Import
作用:用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解。当然,写上也没问
题。
属性:value[]:用于指定其他配置类的字节码。
//示例代码:
/**
* 主配置类,相当于bean.xml文件
* */
@Configuration
@ComponentScan("com.zy.account")//包扫描
@Import(value = JdbcConfig.class)//导入子配置类
public class SpringConfiguration {
}
/**
*子配置类
*/
public class JdbcConfig {
@Bean(name = "queryRunner")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
@Bean(name = "dataSources")
public DataSource createDataSources(){
try {
ComboPooledDataSource dataSources = new ComboPooledDataSource();
dataSources.setDriverClass("com.mysql.jdbc.Driver");
dataSources.setJdbcUrl("jdbc:mysql://localhost:3306/stu");
dataSources.setUser("root");
dataSources.setPassword("123456");
return dataSources;
}catch (Exception e){
throw new RuntimeException();
}
}
}
@PropertySource
/*作用:用于加载.properties 文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到
properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。
属性:value[]用于指定 properties 文件位置。如果是在类路径下,需要写上 classpath:
jdbc.properties文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stu
jdbc.username=root
jdbc.password=123456*/
//示例代码:
//配置类
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean(name = "queryRunner")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
@Bean(name = "dataSources")
public DataSource createDataSources(){
try {
ComboPooledDataSource dataSources = new ComboPooledDataSource();
dataSources.setDriverClass(driver);
dataSources.setJdbcUrl(url);
dataSources.setUser(username);
dataSources.setPassword(password);
return dataSources;
}catch (Exception e){
throw new RuntimeException();
}
}
}
/**
* 主配置类,相当于bean.xml文件
* */
@Configuration
@ComponentScan("com.zy.account")//包扫描
@Import(value = {
JdbcConfig.class})//导入子配置类
@PropertySource(value = "classpath:jdbc.properties")//加载properties配置文件
public class SpringConfiguration {
}
Spring整合Junit
导入整合的jar包。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
测试代码(配置文件方式),这种方式是我们使用xml方式开发,有spring的配置文件:
说明@ContextConfiguration 注解两种用法:
locations 属性:用于指定配置文件的位置。如果是类路径下,需要用 classpath表示在类路径下。
classes 属性:用于指定注解的类。当不使用 xml 配置时也就是使用注解开发,需要用此属性指定注解类的位置。
@RunWith(SpringJUnit4ClassRunner.class)//把Junit的main方法替换成Spring提供的
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private AccountServie accountServie;
@Test
public void testSelectAll(){
/*ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
AccountServie accountServie = (AccountServie) context.getBean("accountServie");*/
List<Account> list = accountServie.selectAll();
for (Account accounts:list){
System.out.println(accounts);
}
}
}
测试代码(纯注解方式):
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
private AccountServie accountServie;
@Test
public void testSelectAll(){
/*ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
AccountServie accountServie = (AccountServie) context.getBean("accountServie");*/
List<Account> list = accountServie.selectAll();
for (Account accounts:list){
System.out.println(accounts);
}
}
}
运行结果:
加油吧!!!