Spring的事务管理:声明式事务管理(切面)

声明式事务管理:(自动代理.基于切面)

 

第一步:导入相应jar包.

 aspectj

第二步:引入相应约束:

* aop、tx约束

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">

第三步:注册事务管理器;

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

第四步:定义增强(事务管理)

	<!-- 定义一个增强 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 增强(事务)的属性的配置 -->
		<tx:attributes>
			<!-- 
				isolation:DEFAULT	:事务的隔离级别.
				propagation			:事务的传播行为.
				read-only			:false.不是只读
				timeout				:-1
				no-rollback-for		:发生哪些异常不回滚
				rollback-for		:发生哪些异常回滚事务
			 -->
			<tx:method name="transfer"/>
		</tx:attributes>
	</tx:advice>

第五步:定义aop的配置(切点和通知的组合)

<!-- aop配置定义切面和切点的信息 -->
<aop:config>
<!-- 定义切点:哪些类的哪些方法应用增强 -->
<aop:pointcut expression="execution(* cn.itcast.spring3.demo3.AccountService+.*(..))" id="mypointcut"/>
<!-- 定义切面: -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
</aop:config>

声明式事务管理--完整版配置文件

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 引入外部属性文件. -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<!-- 业务层类 -->
	<bean id="accountService" class="cn.itcast.spring3.demo3.AccountServiceImpl">
		<!-- 在业务层注入Dao -->
		<property name="accountDao" ref="accountDao"/>
	</bean>
	
	<!-- 持久层类 -->
	<bean id="accountDao" class="cn.itcast.spring3.demo3.AccountDaoImpl">
		<!-- 注入连接池的对象,通过连接池对象创建模板. -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 定义一个增强 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 增强(事务)的属性的配置 -->
		<tx:attributes>
			<!-- 
				isolation:DEFAULT	:事务的隔离级别.
				propagation			:事务的传播行为.
				read-only			:false.不是只读
				timeout				:-1
				no-rollback-for		:发生哪些异常不回滚
				rollback-for		:发生哪些异常回滚事务
			 -->
			<tx:method name="transfer"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- aop配置定义切面和切点的信息 -->
	<aop:config>
		<!-- 定义切点:哪些类的哪些方法应用增强 -->
		<aop:pointcut expression="execution(* cn.itcast.spring3.demo3.AccountService+.*(..))" id="mypointcut"/>
		<!-- 定义切面: -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
	</aop:config>
</beans>

第六步:编写测试类:

* 注入Service对象,不需要注入代理对象(生成这个类的时候,已经是代理对象.)

package cn.itcast.spring3.demo3;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 声明式事务使用:基于切面的
 * @author 姜涛
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class SpringTest3 {

	@Autowired
	@Qualifier("accountService")
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("aaa", "bbb", 100d);
	}
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Spring 是一款流行的 Java 开发框架,它提供了许多便利的功能,包括对事务管理的支持。Spring事务管理是建立在底层事务管理器之上的,它为开发人员提供了一种简单的方式来处理事务的提交、回滚、保存点等操作。 Spring 支持两种类型的事务管理:编程式事务管理声明式事务管理。 1. 编程式事务管理 编程式事务管理需要在代码中显式地开启、提交或回滚事务。开发人员需要在代码中使用 Spring 提供的事务模板(TransactionTemplate)类来管理事务事务模板类的主要方法包括: - execute():执行一段代码,并在其中进行事务管理。 - execute(TransactionCallback):执行一个事务回调,该回调负责实际执行数据库操作。 - execute(TransactionCallbackWithoutResult):执行一个没有返回值的事务回调。 使用事务模板类的代码示例: ``` @Autowired private DataSource dataSource; @Transactional public void transferMoney(Account fromAccount, Account toAccount, double amount) { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { try { // 在这里执行数据库操作 // ... } catch (Exception e) { status.setRollbackOnly(); } } }); } ``` 2. 声明式事务管理 声明式事务管理是通过在 Spring 的配置文件中声明事务的属性来管理事务的。这种方式将事务管理的代码从业务代码中分离出来,使代码更加清晰。 声明式事务管理的实现方式是使用 Spring 的 AOP(Aspect Oriented Programming)技术,在代码执行前、后或异常时插入事务管理的代码。在 Spring 中,声明式事务管理主要有两种方式:基于注解和基于 XML 配置。 使用基于注解的声明式事务管理,可以在方法上添加 @Transactional 注解来指定事务的属性,示例代码: ``` @Autowired private DataSource dataSource; @Transactional public void transferMoney(Account fromAccount, Account toAccount, double amount) { // 在这里执行数据库操作 // ... } ``` 使用基于 XML 配置的声明式事务管理,需要在配置文件中定义事务管理器、事务通知等元素,示例代码: ``` <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="transferMoney" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.example.*.*(..))" /> </aop:config> ``` 以上就是 Spring 事务管理的基本概念和使用方法。在实际应用中,根据具体业务需 ### 回答2: Spring 事务管理是指在Spring框架中对数据库操作进行事务管理的一种机制。在数据库操作中,可能会涉及到多个操作,而事务管理可以确保这些操作要么全部成功,要么全部失败,从而保持数据的一致性。 Spring 事务管理的核心原理是基于AOP(面向切面编程)和IOC(控制反转)两个核心概念,通过在方法或类上添加@Transactional注解来实现事务的控制。 Spring 事务管理的特点如下: 1. 声明式事务管理:可以通过使用@Transactional注解来声明事务,而不需要在代码中显式地编写事务管理代码,简化了操作和维护。 2. 事务的传播特性:可以通过设置事务的传播特性来控制事务的范围和行为,在多个事务操作中灵活进行事务的传递和管理。 3. 事务的隔离级别:可以通过设置事务的隔离级别来控制多个事务之间的隔离程度,从而保证在并发环境下的数据一致性和并发控制。 4. 事务的回滚机制:可以通过设置事务的异常回滚策略,来控制事务在出现异常情况下的回滚和恢复,确保数据的完整性。 5. 多数据源的事务管理Spring支持同时管理多个数据源的事务,可以保证多个数据源之间的数据一致性。 总的来说,Spring事务管理提供了一个方便和可靠的机制,用于管理数据库操作的事务,减少了代码的冗余和复杂度,提高了开发效率和数据的一致性。同时,Spring事务管理的可扩展性和灵活性也使其适用于不同的应用场景和需求。 ### 回答3: Spring事务管理是指通过Spring框架提供的事务管理功能来管理数据库事务的机制。Spring事务管理可以确保数据的一致性和完整性,同时也可以提高数据库的性能和并发能力。 Spring事务管理的特点包括: 1. 声明式事务管理:通过在配置文件或注解中声明事务的属性和规则来管理事务,而不需要手动编写事务管理的代码。这样可以有效地降低代码的复杂性和维护成本。 2. 支持多种事务管理策略:Spring事务管理支持多种事务管理策略,包括本地事务管理和分布式事务管理。可以根据具体的应用场景选择不同的事务管理策略。 3. 支持多种事务传播行为:Spring事务管理支持多种事务传播行为,包括REQUIRED、REQUIRES_NEW、NESTED等。可以根据具体的业务需求来选择不同的事务传播行为。 4. 异常回滚和异常捕获:Spring事务管理可以根据配置的属性来决定事务的回滚行为,包括回滚特定的异常和回滚全部的异常。同时,也可以通过异常捕获来处理事务中的异常情况。 5. 提供事务管理的模板:Spring框架提供了用于事务管理的模板类,可以方便地进行事务管理的操作,如开启事务、提交事务、回滚事务等。 总之,Spring事务管理是一种方便、灵活且可扩展的事务管理机制,可以帮助开发人员简化事务管理的操作,提高代码的可读性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琦彦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值