Spring的注解需要在配置文件中指定需要扫描的包.具体配置参照:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--告知spring在创建容器时候需要扫描的包, 配置所需要的标签不是在bean约束中,而是在一个名为context的名称空间和约束中.--> <context:component-scan base-package="com.mine"></context:component-scan> </beans>在配置文件中:
@Configuration @ComponentScan("com.mine") @PropertySource("jdbcConfig.properties") public class SpringConfiguration { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.user}") private String user; @Value("${jdbc.password}") private String password; /** * 创建一个QueryRunner对象 * @param dataSource * @return */ @Bean(name = "runner") @Scope("prototype") public QueryRunner createQueryRunner (DataSource dataSource) { return new QueryRunner(dataSource); } @Bean(name ="dataSource") public DataSource createDataSource() { try { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(driver); dataSource.setJdbcUrl(url); dataSource.setUser(user); dataSource.setPassword(password); return dataSource; } catch (Exception e){ throw new RuntimeException(); } } }同时在测试模块需要修改读取注解的方法:
ApplicationContext cls = new AnnotationConfigApplicationContext(SpringConfiguration.class);这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常.所以又不能轻易删掉.
为什么不把测试类配到 xml 中? 配置到xml当然可以正常使用, 只是因为: ①当我们在 xml 中配置了一个 bean,spring 加载配置文件创建容器时,就会创建对象. ②测试类只是我们在测试功能时使用,而在项目中它并不参与程序逻辑,也不会解决需求上的问题,所以创建完了,并没有使用. 那么存在容器中就会造成资源的浪费. 所以,我们不应该把测试配置到 xml 文件中.
集成
导入spring整合junit的jar包(坐标) <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.7.RELEASE</version> </dependency>2)使用junit提供的一个main方法把原有的main方法替换掉, 替换成spring提供的@Runwith.
@RunWith(SpringJUnit4ClassRunner.class)之所以需要替换,是以为junit单元测试中, 没有main方法也能执行, junit本身集成了一个main方法, 该方法会判断当前测试类中哪些方法包含@Test的注解,junit就会让这些带有@Test的方法执行. 而junit本身并不会关注我们的程序是否集成了spring,所以在执行测试方法时候, 它也不会为我们读取配置文件或配置类来为我们创建容器.查看源码可知, SpringJUnit4ClassRunner其本身也是继承了junit. 虽然junit不会为我们创建容器,由于SpringJUnit4ClassRunner是spring提供,所以它一定会为我们创建容器. 3)告知spring的运行期,spring的Ioc创建是基于xml还是注解.并且通过@ContextConfiguration 注解来说明位置. @ContextConfiguration 注解: 其中, locations 属性用于指定配置文件的位置. 如果是类路径下,需要用 classpath表明.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations= {"classpath:bean.xml"}) public class AccountServiceTest { }classes 属性用于指定注解的类, 当不使用 xml 配置时,需要用此属性指定注解类的位置.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfiguration.class) public class test { }4)通过以上三步的配置, 在后续的单元测试中, 可以不需要在重复写创建容器的代码,简单配置即可.实例如下:
// 替换原有的main方法 @RunWith(SpringJUnit4ClassRunner.class) // 指定注解类的位置 @ContextConfiguration(classes = SpringConfiguration.class) public class test { // 注入 @Autowired private IAccountService accountService; @Test public void findAllAccount() { List<Account> allAccount = accountService.findAllAccount(); for (Account account : allAccount) { System.out.println(account); } } }当使用spring 5.x版本时候, 需要junit的包必须是4.12以上,否则会报错.