Spring中配备自动的事务代理
Spring中配置自动的事务代理
公司项目中配置了Spring 的事务代理, 不用在数据库操作的时候麻烦写很多的事务开始和回滚的代码了。
<!--事务管理器--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!--这里定义拦截所有的方法--> <bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="txManager"/> </property> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> <!--<prop key="update">PROPAGATION_REQUIRED</prop>--> <!--<prop key="delete">PROPAGATION_REQUIRED</prop>--> </props> </property> </bean> <!--拦截所有名称以Service结束的类--> <bean id="txBeanNameProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <value>txInterceptor</value> </list> </property> <property name="beanNames"> <value>*Service</value> </property> </bean>
详解见另外一篇博客:
http://taoistwar.iteye.com/blog/323729