java中的接口是类吗
224
2023-04-27
详解Spring学习之声明式事务管理
前言
在前面的小节中,我们学习了关于事务的概念以及事务管理的重要性,并且通过编程使用Spring的编程式事务管理进行操作,加深对事务管理的重要性的学习,不过,由于编程式的事务管理使用起来不是很方便,所以在日常的开发中基本不怎么使用,接下来的内容我们将学习使用Spring的声明式事务管理,这里有一个地方需要明白的是,Spring的声明式事务管理的实现方式其实是通过AOP的方式来实现的,也就是为原始的事务管理对象创建代理对象,从而实现事务管理增强的
基于TransactionProxyFactoryBean的事务管理配置
经过前面的学习,可以知道,Spring中配置AOP有三种方式,分别是通过ProxyFactoryBean创建代理,通过XML的方式以及通过注解的方式,既然Spring事务管理是通过AOP来实现的,那么对应的就有三种不同的方式,首先来看下基于TransactionProxyFactoryBean的管理方式
首先是Spring的配置文件
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
对应的持久层代码
@Repository
public class AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void transferIn(String name, double money){
String sql = "update account set money = money + ? where name = ?";
jdbcTemplate.update(sql, money, name);
}
public void transferOut(String name, double money){
String sql = "update account set money = money - ? where name = ?";
jdbcTemplate.update(sql, money, name);
}
}
业务层代码
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
public void transfer(final String fromName,final String toName,final double money){
accountDao.transferOut(fromName, money);
int d = 1/0; // 除0异常
accountDao.transferIn(toName, money);
}
}
通过上面的配置之后,当我们在使用AccountService的时候,由于获取的对象的代理后的对象,所以Spring会自动进行事务的监管,而我们需要做的就是配置对应的事务传播类型以及事务管理级别等的信息,这种方式明显对代码以及没有什么侵入了,但是使用这种方式意味着没有都需要为不同的服务对象创建对应的代理对象,这其实是不太方便的,接下来我们来看下使用aop/tx命名空间来进行配置的方式。
基于aop/tx命名空间的事务管理配置
由于是对上面的业务操作进行事务管理,而且经过上一小节的学习,我们也基本熟悉了该业务,所以这里直接演示配置的代码
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd">
可以看到,通过XML配置的方式,可以更加灵活地进行事务管理
基于注解的事务管理配置
基于注解的配置方式提供了更加简单的配置http://方式,只需要使用@Transactional注解进行标注,并且开启对应的扫描即可。
// 配置相应的隔离级别、事务传播等
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
@Service
public class AccountService {
// 省略其他内容
}
Spring配置文件也相对比较简单了
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd">
可以看到,通过注解配置的方式是最简单的配置方式,在日常的开发中,这种方式的使用的频率也比较高
总结
本小节主要学习了Spring声明式事务管理的配置,包括了使用TransactionProxyFactoryBean、通过aop/tx命名空间的XML配置以及基于注解的配置方式,其中,基于注解的配置方式是比较简单的,也是使用频率比较高的一种
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~