SPRING JDBC事务管理的三种配置方法
一.???? 一般的JDBC事务,通常可以这样处理:
<bean id="txProxyTemplate" abstract="true"???
parent="txProxyTemplate">???
??? <property name="target">???
??????? <bean parent="txProxyTemplate" >???
??? <property name="target">???
??????? <bean />
?? </property>
</bean>
?
<bean id="transactionInterceptor" ref="transactionManager"/>
?? <property name="transactionAttributes">
??? <props>
???? <prop key="*">PROPAGATION_REQUIRED</prop>
???? <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
???? <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
???? <prop key="request*">PROPAGATION_REQUIRED,readOnly</prop>
??? </props>
?? </property>
</bean>
<bean ref="transactionInterceptor"/>
</bean>
关键在上面的高亮处,只要类的名字满足*Service的规则,那么它就会对这个类进行事务管理!
如此,我们无需再添加累赘的代码,只要名字满足规则就ok了!
?
?
?
三.第三种 tx:advice 和 aop:config 简化配置事务:
??? <tx:advice id="txAdvice" transaction-manager="transactionManager">
??????? <tx:attributes>
??????????? <tx:method name="add*" propagation="REQUIRED" />
??????????? <tx:method name="delete*" propagation="REQUIRED" />
??????????? <tx:method name="update*" propagation="REQUIRED" />
??????????? <tx:method name="add*" propagation="REQUIRED" />
??????????? <!-- <tx:method name="*" propagation="true" />-->
??????? </tx:attributes>
??? </tx:advice>
?
?
??? <aop:config>
??????? <aop:pointcut id="allManagerMethod"
??????????? expression="execution(* com.service.*.*(..))" />
??????? <aop:advisor advice-ref="txAdvice"
??????????? pointcut-ref="allManagerMethod" />
??? </aop:config>
?
解释一下(* com.evan.crm.service.*.*(..))中几个通配符的含义:
第一个 * —— 通配 任意返回值类型
第二个 * —— 通配 包com.evan.crm.service下的任意class
第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法
第四个 .. —— 通配 方法可以有0个或多个参数