在 spring 中, 事务管理一般是通过声明一个 txProxyTemplate, 然后业务 bean 中 parent = "txProxyTemplate", 这样做未免显得有些繁琐, 并且如果业务 bean 还需要其他拦截器, 配置也不太方便, 下面贴出我的配置, 用 DefaultAdvisorAutoProxyCreator 实现自动代理
- <beans>
- <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
- <bean id= "transactionManager"
- class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
- <property name= "sessionFactory" >
- <ref bean= "sessionFactory" />
- </property>
- </bean>
- <bean id= "transactionInterceptor" class = "org.springframework.transaction.interceptor.TransactionInterceptor" >
- <property name= "transactionManager" ref= "transactionManager" />
- <property name= "transactionAttributeSource" >
- <value>
- com.skyon.user.manager.UserManager.*=PROPAGATION_REQUIRED
- #Add new defines here ->
- </value>
- </property>
- </bean>
- <bean class = "org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" >
- <property name= "interceptorNames" >
- <list>
- <value>transactionInterceptor</value>
- <!--
- 增加新的 Interceptor
- -->
- </list>
- </property>
- </bean>
- <bean class = "org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor" >
- <property name= "transactionInterceptor" ref= "transactionInterceptor" />
- </bean>
- </beans>
这里利用 DefaultAdvisorAutoProxyCreator 实现了对容器中所有 bean 的自动代理, 增加一个需要事务的业务 bean 时只要在 transactionInterceptor 增加一行即可, 增加别的 interceptor 也非常方便,
极大减少了配置量