zoukankan      html  css  js  c++  java
  • Spring基于配置的事务控制

    准备jar包
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.7</version>
    </dependency>

    开始配置
    <?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:context="http://www.springframework.org/schema/context"
        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/aop
          https://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context
          https://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/tx
          https://www.springframework.org/schema/tx/spring-tx.xsd">
      <!--配置扫描路径-->
      <context:component-scan base-package="com.aiitec"></context:component-scan>

      

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

        <!--配置事务的通知-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
          <!--配置事务的属性
            isolation:事务的隔离级别,默认为数据库的默认级别
            propagation:用于指定事务的传播行为。默认值是REQUIRED(增删改使用),表示一定会有事务,读取方法使用SUPPORTS
            read-only:用于指定事务是否只读。默认是false,表示读写,读取方法才能使用true
            timeout:指定事务的超时时间,默认值-1,表示永不超时。如果指定数值,单位为秒。
            no-rollback-for:指定一个异常,如果发生该异常则不回滚,其余异常都回滚。不写则默认所有异常都回滚
            rollback-for:指定一个异常,如果发生该异常则回滚,其余异常都不回滚。不写则默认所有异常都回滚
          -->
          <tx:attributes >
            <tx:method name="*" propagation="REQUIRED" read-only="false"></tx:method>
            <!--以下为部分匹配,优先级会高于通配,所以查询方法必须遵循命名规则-->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>

          </tx:attributes>
        </tx:advice>

        <!--配置切入点表达式-->
        <aop:config>
          <aop:pointcut id="pointCut" expression="execution(* com.aiitec.service.impl.*.*(..))"></aop:pointcut>
          <!--建立切入点表达式和事务通知的对应关系-->
          <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"></aop:advisor>
        </aop:config>
      <!--事务控制end-->

      <!--配置spring-jdbc-template数据源 begin-->
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
      </bean>

      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
      </bean>
      <!--配置spring-jdbc-template数据源 end-->

  • 相关阅读:
    JSP(一)
    设计模式之UML类图
    Servle原理
    Servlet 浅谈(三)
    Servlet 浅谈(二)
    Servlet 浅谈(一)
    闲聊
    设计模式之装饰器模式
    struts2源码调试环境的搭建
    Github学习
  • 原文地址:https://www.cnblogs.com/zou-rong/p/12047768.html
Copyright © 2011-2022 走看看