【spring补充】spring-ibais事务配置
代理实现
<bean id="userServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="target"> <ref local="userService" /> </property> <property name="transactionAttributes"> <props> <!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 --> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>
拦截器实现
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
AOP和TX配置
<!-- 需要引入aop的命名空间 -->
<aop:config>
<!-- 切入点指明了在所有方法产生事务拦截操作 -->
<aop:pointcut id="serviceMethods"
expression="execution(* com.angi.ibatis.service.*.*(..))" />
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<!-- 需要引入tx的命名空间 -->
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
anotation
<!-- 需要引入tx的命名空间 -->
<tx:annotation-driven transaction-manager="transactionManager" />
@Transactional
public void doTransaction() {
User user = new User();
user.setName("11111");
user.setSex(1);
userDao.saveUser(user);
User user1 = new User();
user1.setName("Angikkk");
user1.setSex(1);
userDao.saveUser(user1);
链接:http://www.cnblogs.com/Angi/articles/2007563.html

浙公网安备 33010602011771号