Spring学习5_通过XML进行事务配置
Spring的AOP的一个重要应用就是用来进行事务管控,本例通过结合Hibernate的事务管控,用一个简单Demo来模拟其实现:
具体如下:
1、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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/hibernate"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="mappingResources"> <list> <value>student.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true </value> </property> </bean> <bean id = "dao" class="com.dao.UserDaoImpl"> <!-- <property name="sessionFactory" ref="mySessionFactory"></property> --> <property name="txManager" ref="txManager"></property> </bean> <bean id="userService" class="com.service.UserService"> <property name="dao" ref="dao"></property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="mySessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <!-- other methods use the default transaction settings (see below) --> <tx:method name="*" rollback-for="Throwable" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="userServiceOperation" expression="execution(* com.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="userServiceOperation"/> </aop:config> </beans>
2、Dao层实现如下
package com.dao; import org.hibernate.Session; import org.springframework.orm.hibernate4.HibernateTransactionManager; import com.vo.Student; public class UserDaoImpl implements UserDao { private HibernateTransactionManager txManager; @Override public void save(Student student) { Session session = txManager.getSessionFactory().getCurrentSession(); session.save(student); System.out.println("excute save;"); } public void setTxManager(HibernateTransactionManager txManager) { this.txManager = txManager; } @Override public void delete() { System.out.println("excute delete;"); } }
3、Service层
package com.service; import com.dao.UserDao; import com.vo.Student; public class UserService { private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; } public void save() throws Exception{ Student student = new Student(3, "test3", 30); dao.save(student); Student student2 = new Student(4, "test4", 30); dao.save(student2); //throw new Exception(); } }
测试1:当上述Service中异常被注释掉时,执行后数据库正常插入
测试2:将数据库中数据清除,同时将Service中注释代码去掉,执行后抛出异常,数据回滚,插入失败,如下:
事务管控生效。