标签:ntc man import span component user model com text
1.com.cy.model中User.java 和 Log.java 实体 和上一节一样;
2.DAO的实现类com.cy.dao.impl中的UserDAOImpl、LogDAOImpl.java和上一节一样;
3.UserService.java:
package com.cy.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.cy.dao.LogDAO; import com.cy.dao.UserDAO; import com.cy.model.Log; import com.cy.model.User; @Component("userService") public class UserService { @Resource private UserDAO userDAO; @Resource private LogDAO logDAO; public void init() { System.out.println("init"); } public User getUser(int id) { return null; } /** * 插入一个用户的同时,记录一条日志 */ public void add(User user) { userDAO.save(user); Log log = new Log(); log.setMsg("a user saved!"); logDAO.save(log); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
4.beans.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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"> <context:annotation-config /> <context:component-scan base-package="com.cy"/> <!-- 如果下面使用到了占位符的方式,请在locations下面去找 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 初始化SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.cy.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <aop:config> <aop:pointcut id="bussinessService" expression="execution(public * com.cy.service..*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="getUser" read-only="true" /> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
5.测试代码:
package com.cy.service; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cy.model.User; public class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); System.out.println(service.getClass()); service.add(new User()); ctx.destroy(); } }
1)正常情况下测试,没有异常抛出,ok,user和log都插入;
2)在LogDAOImpl中的save方法中手动抛出一个异常,插入失败;user没有插入成功,已经回滚了。
标签:ntc man import span component user model com text
原文地址:http://www.cnblogs.com/tenWood/p/6892197.html