XML配置事务管理

1:在spring配置文件中进行配置

image.png

  1. <!-- 开启组件扫描 -->
  2. <context:component-scan
  3. base-package="com.junjay.spring5"></context:component-scan>
  4. <!-- 直接通过druidjar配置数据库链接 -->
  5. <bean id="dataSource"
  6. class="com.alibaba.druid.pool.DruidDataSource">
  7. <!-- 直接配置 *********************************************************** -->
  8. <property name="driverClassName"
  9. value="com.mysql.jdbc.Driver"></property>
  10. <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
  11. <property name="username" value="root"></property>
  12. <property name="password" value="root"></property>
  13. </bean>
  14. <!-- 创建JdbcTemplate对象 -->
  15. <bean id="jdbcTemplate"
  16. class="org.springframework.jdbc.core.JdbcTemplate">
  17. <!-- 用set注入dataSource数据源 -->
  18. <property name="dataSource" ref="dataSource"></property>
  19. </bean>
  20. <!-- 1: 创建事务管理器 -->
  21. <bean id="transactionManager"
  22. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  23. <!-- 注入数据源 -->
  24. <property name="dataSource" ref="dataSource"></property>
  25. </bean>

2:配置通知

  1. <!-- 2: 配置事务通知 -->
  2. <tx:advice id="txadvice">
  3. <!-- 配置事务参数 -->
  4. <tx:attributes>
  5. <!-- 配置事务那种规则方法上加配置 -->
  6. <!-- com.junjay.spring5.service.UserService.transfer(); 可以直接将方法配置 trans*
  7. : 表示以trans方法名开头的都进行配置 -->
  8. <tx:method name="transfer" read-only="false" timeout="-1"
  9. propagation="REQUIRED" />
  10. <!-- <tx:method name="trans*"/> -->
  11. </tx:attributes>
  12. </tx:advice>

3:配置切入点和切面

  1. <!-- 3: 配置事务切入点和切面 -->
  2. <aop:config>
  3. <!-- 配置切入点,配置UserService下的所有方法都加上以上事务管理 -->
  4. <aop:pointcut
  5. expression="execution(* com.junjay.spring5.service.UserService.*(..))"
  6. id="pt" />
  7. <!-- 配置切面 advice-ref:通知名字 pointcut-ref:切入点名字 -->
  8. <aop:advisor advice-ref="txadvice" pointcut-ref="pt" />
  9. </aop:config>

4:完整xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util.xsd
  20. ">
  21. <!-- 开启组件扫描 -->
  22. <context:component-scan
  23. base-package="com.junjay.spring5"></context:component-scan>
  24. <!-- 直接通过druidjar配置数据库链接 -->
  25. <bean id="dataSource"
  26. class="com.alibaba.druid.pool.DruidDataSource">
  27. <!-- 直接配置 *********************************************************** -->
  28. <property name="driverClassName"
  29. value="com.mysql.jdbc.Driver"></property>
  30. <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
  31. <property name="username" value="root"></property>
  32. <property name="password" value="root"></property>
  33. </bean>
  34. <!-- 创建JdbcTemplate对象 -->
  35. <bean id="jdbcTemplate"
  36. class="org.springframework.jdbc.core.JdbcTemplate">
  37. <!-- 用set注入dataSource数据源 -->
  38. <property name="dataSource" ref="dataSource"></property>
  39. </bean>
  40. <!-- 1: 创建事务管理器 -->
  41. <bean id="transactionManager"
  42. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  43. <!-- 注入数据源 -->
  44. <property name="dataSource" ref="dataSource"></property>
  45. </bean>
  46. <!-- 2: 配置事务通知 -->
  47. <tx:advice id="txadvice">
  48. <!-- 配置事务参数 -->
  49. <tx:attributes>
  50. <!-- 配置事务那种规则方法上加配置 -->
  51. <!-- com.junjay.spring5.service.UserService.transfer(); 可以直接将方法配置 trans*
  52. : 表示以trans方法名开头的都进行配置 -->
  53. <tx:method name="transfer" read-only="false" timeout="-1"
  54. propagation="REQUIRED" />
  55. <!-- <tx:method name="trans*"/> -->
  56. </tx:attributes>
  57. </tx:advice>
  58. <!-- 3: 配置事务切入点和切面 -->
  59. <aop:config>
  60. <!-- 配置切入点,配置UserService下的所有方法都加上以上事务管理 -->
  61. <aop:pointcut
  62. expression="execution(* com.junjay.spring5.service.UserService.*(..))"
  63. id="pt" />
  64. <!-- 配置切面 advice-ref:通知名字 pointcut-ref:切入点名字 -->
  65. <aop:advisor advice-ref="txadvice" pointcut-ref="pt" />
  66. </aop:config>
  67. </beans>

5:测试image.png