Spring配置声明式事务
Spring配置声明式事务
1、xml方式
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="transfer" isolation="DEFAULT" timeout="-1" read-only="false" propagation="REQUIRED" no-rollback-for="" rollback-for=""/> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <aop:pointcut id="myPointcut" expression="bean(*Service)"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/> </aop:config>
2、注解方式
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
在类或方法上标注:@Transactional(rollbackFor={Exception.class})
注意:两种方法都配置了 proxy-target-class="true"
如果不配置,则默认情况下,有接口时使用jdk代理,无接口时使用cglib代理,如果有接口又注入子类时就会报错。
配置这个属性后表示强制使用cglib代理。