@Configuration 作用:指定当前类是一个配置类,等同于applicationContext.xml @ComponentScan 作用:用于通过注解指定spring在创建容器时要扫描的包 属性:value,basePackages(两个属性别名互相引用,所以作用相同)指定要扫描的包 使用注解@ComponentScan(basePackages = {"com.study"})作用等同于 在xml中配置了<context:component-scan base-package="com.study"></context:component-scan> @Bean 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中 属性:name:用于指定bean的id,当不写时,默认值时当前方法的名称 细节:当使用注解配置方法时,如果方法有参数,spring会去容器中查找有没有可用的bean对象 查找的方式和Autowired相同,根据类型匹配,有一个注入成功,没有注入失败,有多个会找bean的id和该参数名相同的进行匹配 如果有多个,可以在参数前使用@Qualifier("")注解指定容器中的bean(单独使用的情况) @Bean public JdbcTemplate createJdbcTemplate(@Qualifier("ds1") DataSource dataSource){ return new JdbcTemplate(dataSource); } @Scope 作用:声明此bean对象是多例的 @Import 作用:导入其他配置类 参数:value:用于指定其他配置类的字节码 当我们使用@Import后,当前类为主配置类,导入的为子配置类 @PropertySource 作用:用于指定properties文件的位置 属性:value:指定文件的名称和路径 关键字:classpath:表示类路径下
spring整合junit
使用junit测试spring的项目时,由于不会创建spring的容器,所以会出现一些问题 所以spring整合了junit,使用步骤 1导入spring整合junit的jar包(坐标) spring-test坐标 2使用junit提供的一个注解把原有的main方法替换了,替换成spring提供的 @RunWith @RunWith(SpringJUnit4ClassRunner.class) 3告知spring的运行器,spring的ioc容器是基于xml还是注解的,并且说明位置 注解 @ContextConfiguration 属性 location:指定xml文件的位置,加上classpath关键字,表示在类路径下 classes:指定注解类所在的位置 当spring的版本为5.x版本的时候,要求junit的jar必须是4.12及以上 例子: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(location="classpath:xxxx.xml")或者@ContextConfiguration(classes=xxxx.class) public class XXTest{ @Autowired private XXX xx; @Test public void xxTest(){ System.out.println("测试"); } }
简单例子:
配置类
package config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @Configuration @ComponentScan(basePackages = {"com.study"}) //可以简写为@ComponentScan("com.study") @PropertySource("classpath:db.properties") public class SpringConfig { @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 id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> * <constructor-arg name="dataSource" ref="dataSource"></constructor-arg> * </bean> */ @Bean public JdbcTemplate createJdbcTemplate(DataSource dataSource){ return new JdbcTemplate(dataSource); } /** *替换配置文件中的 * <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> * <property name="driverClassName" value="${jdbc.driver}"></property> * <property name="url" value="${jdbc.url}"></property> * <property name="username" value="${jdbc.username}"></property> * <property name="password" value="${jdbc.password}"></property> * </bean> */ @Bean public DataSource createDataSource() throws Exception { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
测试方法
package com.study.test; import com.study.model.Account; import com.study.service.AccountService; import config.SpringConfig; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.List; public class SpringTest2 { @Test public void test1(){ //xml配置方式获取spring容器对象 /*ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");*/ //注解方式获取spring容器对象 ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class); AccountService service= ac.getBean("accountService",AccountService.class); List<Account> list = service.findAll(); System.out.println(list); } }