spring整合 事务

本文详细介绍了Spring框架中事务管理的实现方式,包括通过注解和声明式事务进行配置的方法。讲解了如何添加AOP和AspectJ依赖,配置事务管理器,并使用@Transactional注解和XML配置来管理事务。

一、spring整合 事务

  • 注解方式

    1. 添加Aop依赖

      <!-- spring aop支持 -->
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-aop</artifactId>
      	<version>${spring.version}</version>
      </dependency>
      <!-- aspectj支持 -->
      <dependency>
      	<groupId>org.aspectj</groupId>
      	<artifactId>aspectjweaver</artifactId>
      	<version>1.8.7</version>
      </dependency>
      
    2. 添加事务管理器

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans
      				http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      				http://www.springframework.org/schema/context
      				http://www.springframework.org/schema/context/spring-context-4.3.xsd
      				http://www.springframework.org/schema/tx
          			http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
      				http://www.springframework.org/schema/aop
      				http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
       
      	<!-- 定义事务管理器 -->
      	<bean id="transactionManager" 		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      		<property name="dataSource" ref="数据源名称" />
      	</bean>
      	<!--使用注释事务 -->
      	<tx:annotation-driven transaction-manager="transactionManager" />
      </beans>
      
    3. 添加@Transactional

  • 声明式事务

    1. 添加依赖

      <!-- spring aop支持 -->
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-aop</artifactId>
      	<version>${spring.version}</version>
      </dependency>
      <!-- aspectj支持 -->
      <dependency>
      	<groupId>org.aspectj</groupId>
      	<artifactId>aspectjweaver</artifactId>
      	<version>1.8.7</version>
      </dependency>
      
    2. 配置事务管理器

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans
      				http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      				http://www.springframework.org/schema/context
      				http://www.springframework.org/schema/context/spring-context-4.3.xsd
      				http://www.springframework.org/schema/tx
          			http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
      				http://www.springframework.org/schema/aop
      				http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
       
      	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
      	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      		<property name="dataSource" ref="数据源名称" />
      	</bean>
      	<!--事务通知-->
      	<tx:advice id="txAdvice" transaction-manager="transactionManager">
      		<tx:attributes>
      			<tx:method name="方法名*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
      		 </tx:attributes>
      	</tx:advice>
      	<!-- 自动代理 -->
      	<aop:aspectj-autoproxy proxy-target-class="true" />
      
      	<!-- 事务处理 -->
      	<aop:config>
      		<!--切点-->
      		<aop:pointcut id="pc" expression="表达式" />
      		<!--切点通知-->
      		<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
      	</aop:config>
      </beans>
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值