Spring如何基于xml实现声明式事务控制

一、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>A02spring</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.6</version>
    </dependency>
    <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.8.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

二、spring的xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd">

  <bean id="accountService" class="com.wuxi.services.impl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
  </bean>

  <bean id="accountDao" class="com.wuxi.daos.impl.AccountDaoImpl">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="http://www.cppcns.com/ruanjian/java/com.mysql.cj.jdbc.Driver"></property>
    <property name="url"
         value="http://www.cppcns.com/ruanjian/java/jdbc:mysql://192.168.2.105:3306/ssm?characterEncoding=utf8&useSSL=false"></property>
    <property name="username" value="http://www.cppcns.com/ruanjian/java/root"></property>
    <property name="password" value="http://www.cppcns.com/ruanjian/java/123456"></property>
  </bean>

<!--
spring中基于xml的声明式事务控制配置步骤
  1、配置事务管理器
  2、配置事务的通知
  3、配置aop中的通用切入点表达式
  4、建立事务通知和切入点表达式的对应关系
  5、配置事务的属性
-->
  <!--事务管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>
  <!--事务的通知-->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--
    事务的属性
      isolation:用于指定事务的隔离级别。默认值是DEFAULE,表示使用数据库的默认隔离级别。
      propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTYS。
      read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
      timeout:用于指定事务的超时时间,默认值是-1,表示永不超时,如果指定了数值,以秒为单位。
      rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
      no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。
    -->
    <tx:attributes>
      <tx:method name="*" propagation="REQUIRED" read-only="false"/>
      <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
  </tx:advice>
  <aop:config>
    <!--切入点表达式-->
    <aop:pointcut id="ptc" expression="execution(* com.wuxi.services.*.*(..))"/>
    <!--切入点表达式和事务通知的对应关系-->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="ptc"></aop:advisor>
  </aop:config>

</beans>

Spring如何基于xml实现声明式事务控制

扫一扫手机访问