在XML版中完成注解@Transactional的功能

源码获取github

1.项目结构,数据库表跟注解版一样

2.XML配置

<!--5.配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="druidDataSource"/>
</bean>

<!--6.告知哪些方法是需要被事务管理器进行管理的, 等价于注解版,在方法上面写@Transactional -->
<tx:advice transaction-manager="transactionManager" id="serviceAdvice">
   <tx:attributes>
      <!--哪些方法,也可以设置属性,对应注解版的属性-->
      <tx:method name="get*" read-only="true"/>
      <tx:method name="load*" read-only="true"/>
      <tx:method name="list*" read-only="true"/>
      <tx:method name="find*" read-only="true"/>
      <tx:method name="query*" read-only="true"/>
      <tx:method name="sel*" read-only="true"/>
      <tx:method name="check*" read-only="true"/>
      <tx:method name="valid*" read-only="true"/>
      <tx:method name="login*" read-only="true"/>
      <!--这是方法都是查询的方法,设置为只读-->

      <tx:method name="*" propagation="REQUIRED"/>
   </tx:attributes>
</tx:advice>

<!--7.配置AOP和tx:advice配合使用,告知我哪个层次下的方法会被事务管理器进行管理-->
<aop:config proxy-target-class="true">
   <aop:pointcut id="servicePointCut" expression="execution(* com.hs..service.*Service.*(..))"/>
   <aop:advisor advice-ref="serviceAdvice" pointcut-ref="servicePointCut"/>
</aop:config>
<!--8.启动事务注解:告知该方法是事务方法(一个错,其他全部错),而不是普通方法 transaction-manager="transactionManager"可以省略 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

意思就是:我监控的是com.hs..service.*Service:service层下以Service结尾的类下的:这些方法

 <tx:method name="get" read-only="true"/>
      <tx:method name="load" read-only="true"/>
      <tx:method name="list" read-only="true"/>
      <tx:method name="find" read-only="true"/>
      <tx:method name="query" read-only="true"/>
      <tx:method name="sel" read-only="true"/>
      <tx:method name="check" read-only="true"/>
      <tx:method name="valid" read-only="true"/>
      <tx:method name="login" read-only="true"/>
      <!--这是方法都是查询的方法,设置为只读-->
<tx:method name="*" propagation="REQUIRED"/>

3.其他代码跟注解版一样,只是去掉@Transactional