Spring 3.0 新提供的特性 @Configuration,这个注释标签允许您用 Java 语言来定义 bean 实例。
package config; import org.Springframework.beans.factory.annotation.Autowired; import org.Springframework.context.annotation.Bean; import org.Springframework.context.annotation.Configuration; import org.Springframework.jdbc.datasource.DriverManagerDataSource; import service.AccountService; import service.Initializer; import DAO.AccountDao; @Configuration public class SpringDb2Config { private @Autowired DriverManagerDataSource datasource; @Bean public Initializer initer() { return new Initializer(); } @Bean public AccountDao accountDao() { AccountDao DAO = new AccountDao(); DAO.setDataSource(datasource); return DAO; } @Bean public AccountService accountService() { return new AccountService(); } }通过@ContextConfiguration指定配置文件,Spring test framework 会自动加载 XML 文件,也是我们采用的方式。
<beans xmlns="http://www.Springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.Springframework.org/schema/beans http://www.Springframework.org/schema/beans/Spring-beans-3.2.xsd"> <beans profile="test"> <bean id="datasource" class="org.Springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url" value="jdbc:hsqldb:hsql://localhost" /> <property name="username" value="sa"/> <property name="password" value=""/> </bean> </beans> <beans profile="production"> <bean id="datasource" class="org.Springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url" value="jdbc:hsqldb:hsql://localhost/prod" /> <property name="username" value="sa"/> <property name="password" value=""/> </bean> </beans> <beans profile="test,production"> <bean id="transactionManager" class="org.Springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <bean id="initer" init-method="init" class="service.Initializer"> </bean> <bean id="accountDao" depends-on="initer" class="DAO.AccountDao"> <property name="dataSource" ref="datasource"/> </bean> <bean id="accountService" class="service.AccountService"> </bean> <bean id="envSetter" class="EnvSetter"/> </beans> </beans>上面的定义,我们看到:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/config/Spring-db.xml") @Transactional @ActiveProfiles("test") public class AccountServiceTest { ... }注意上面的 @ActiveProfiles,可以指定一个或者多个 profile,这样我们的测试类就仅仅加载这些名字的 profile 中定义的 bean 实例。
下面看一个配置实例:
添加依赖:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.1.RELEASE</version> </dependency>spring配置:
applicationContext.xml
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="false"> <description>Spring公共配置 </description> <context:component-scan base-package="cn.slimsmart.unit.test.demo" /> </beans>AddServiceImpl添加@service注解
测试类:
package cn.slimsmart.unit.test.demo.junit; import static org.junit.Assert.assertTrue; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; //用来说明此测试类的运行者,这里用了 SpringJUnit4ClassRunner @RunWith(SpringJUnit4ClassRunner.class) //指定 Spring 配置信息的来源,支持指定 XML 文件位置或者 Spring 配置类名 @ContextConfiguration(locations = { "classpath*:/applicationContext.xml" }) //表明此测试类的事务启用,这样所有的测试方案都会自动的 rollback, //@Transactional //defaultRollback,是否回滚,默认为true //transactionManager:指定事务管理器一般在spring配置文件里面配置 //@TransactionConfiguration(defaultRollback=true,transactionManager="transactionManager") //但是需要兼容生产环境的配置和单元测试的配置,那么您可以使用 profile 的方式来定义 beans, //@ActiveProfiles("test") public class JunitSpringTest { @Autowired private AddService addService; @Before public void setUp(){ System.out.println("初始化"); } @Test public void testAdd() { assertTrue(addService.add(1, 1) == 2); } @After public void destroy() { System.out.println("退出,资源释放"); } }