19_Spring_事务管理XML配置方式

发布时间 2023-07-30 12:49:24作者: AidenDong

19_Spring_事务管理XML配置方式

applicationContext中,通过AOP实现事务的控制

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
  3.     xmlns:p="http://www.springframework.org/schema/p"
    
  4.     xmlns:c="http://www.springframework.org/schema/c"
    
  5.     xmlns:util="http://www.springframework.org/schema/util"
    
  6.     xmlns:context="http://www.springframework.org/schema/context"
    
  7.     xmlns:aop="http://www.springframework.org/schema/aop"
    
  8.     xmlns:tx="http://www.springframework.org/schema/tx"
    
  9.    xsi:schemaLocation="
    
  10.    http://www.springframework.org/schema/beans
    
  11.    http://www.springframework.org/schema/beans/spring-beans.xsd
    
  12.    http://www.springframework.org/schema/util
    
  13.    http://www.springframework.org/schema/util/spring-util.xsd
    
  14.    http://www.springframework.org/schema/context
    
  15.    http://www.springframework.org/schema/context/spring-context.xsd
    
  16.    http://www.springframework.org/schema/aop
    
  17.    http://www.springframework.org/schema/aop/spring-aop.xsd
    
  18.    http://www.springframework.org/schema/tx
    
  19.    http://www.springframework.org/schema/tx/spring-tx.xsd
    
  20. ">
  21. <context:component-scan base-package="com.msb"/>
    
  22. <!--读取jdbc配置文件-->
    
  23. <context:property-placeholder location="classpath:jdbc.properties"/>
    
  24. <!--配置德鲁伊连接池-->
    
  25. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    
  26.     <property name="username" value="${jdbc_username}"></property>
    
  27.     <property name="password" value="${jdbc_password}"></property>
    
  28.     <property name="url" value="${jdbc_url}"></property>
    
  29.     <property name="driverClassName" value="${jdbc_driver}"></property>
    
  30. </bean>
    
  31. <!--配置JDBCTemplate对象,并向里面注入DataSource-->
    
  32. <bean id="jdbcTemplate"
    
    class="org.springframework.jdbc.core.JdbcTemplate">
  33.     <!--通过set方法注入连接池-->
    
  34.     <property name="dataSource" ref="dataSource"></property>
    
  35. </bean>
    
  36. <!--配置一个事务管理器-->
    
  37. <bean id="transactionManager"
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  38.     <!--将数据源注入事务管理器-->
    
  39.     <property name="dataSource"  ref="dataSource"></property>
    
  40. </bean>
    
  41. <!--配置通知-->
    
  42. <tx:advice id="txAdvice">
    
  43.         <!--配置事务参数-->
    
  44.         <tx:attributes>
    
  45.             <tx:method name="transMoney" isolation="DEFAULT"
    
    propagation="REQUIRED"/>
  46.         </tx:attributes>
    
  47. </tx:advice>
    
  48. <!--配置AOP-->
    
  49. <aop:config>
    
  50.     <!--配置切入点-->
    
  51.     <aop:pointcut id="pt" expression="execution(*
    
    com.msb.service.AccountService.transMoney(..))"/>
  52.     <!--配置切面-->
    
  53.     <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
    
  54. </aop:config>