Spring自动流入及整合测试
Spring自动注入及整合测试
新项目用了Spring2.5的@Autowired方式,大大的节省了配置文件;下面来试一试:
Dao层,这里省去了所有的getter和setter方法,以后不用拉这么长了:)
Service层,这里省去了所有的getter和setter方法,以后不用拉这么长了:)
XML配置
看这里的spring配置多简,真是为我们省却了不少的配置。
另外再看我们的测试类:
这样就可以了, 测试类也可以注入了,而且不用context.getBean()的方式去取了,多方便; 不过这里要注意的一个地方是,JUnit test如果低版本的话要升级到4.6,另外我的4.8是不行的,后来我改为4.6就可以了。
新项目用了Spring2.5的@Autowired方式,大大的节省了配置文件;下面来试一试:
Dao层,这里省去了所有的getter和setter方法,以后不用拉这么长了:)
@Repository public class UserDaoImpl extends GenericDaoJpa<User, Long> implements UserDao { public UserDaoImpl() { super(User.class); } }
Service层,这里省去了所有的getter和setter方法,以后不用拉这么长了:)
@Service @Transactional(rollbackFor = Exception.class) public class UserServiceImpl implements IUserService { @Autowired private UserDao userDao; @Autowired private GroupDao groupDao; }
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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 数据源配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${DB.DRIVERCLASSNAME}</value> </property> <property name="url"> <value>${DB.URL}</value> </property> <property name="username"> <value>${DB.USERNAME}</value> </property> <property name="password"> <value>${DB.PASSWORD}</value> </property> </bean> <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- <value>classpath:config.properties</value> --> <value>file:${user.home}/config.properties </value> </list> </property> <property name="ignoreResourceNotFound" value="false" /> </bean> <!-- JPA EntityManagerFactoryBean for EntityManager --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="generateDdl" value="true" /> <property name="databasePlatform" value="org.hibernate.dialect.DB2Dialect" /> </bean> </property> </bean> <!-- Transaction manager for JPA --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory"> <ref bean="entityManagerFactory" /> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- Spring Annotation --> <context:annotation-config /> <!-- Spring auto scan base package --> <context:component-scan base-package="com.test" /> <-- 这里是包名,还有其它参数可配,这里就不详解了--> </beans>
看这里的spring配置多简,真是为我们省却了不少的配置。
另外再看我们的测试类:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:core-context.xml"}) public class ChatTest extends TestCase { @Autowired private IUserService userService; @Test private void testSpring(){ List<User> userList = userService.getAllUser(); if(userList!=null){ System.out.println(userList.size()); } }
这样就可以了, 测试类也可以注入了,而且不用context.getBean()的方式去取了,多方便; 不过这里要注意的一个地方是,JUnit test如果低版本的话要升级到4.6,另外我的4.8是不行的,后来我改为4.6就可以了。