【Spring】—AOP之AspectJ注解方式实现声明式事务管理 - Go语言中文社区

【Spring】—AOP之AspectJ注解方式实现声明式事务管理


前言

    前面博客说到xml方式实现声明式事务管理,这回来说下注解方式的声明式事务管理。

正文

Demo

1、引入相关的jar包

这里写图片描述

2、引入AOP约束

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

3、准备目标对象

public interface AccountService {
   //转账方法
    void transfer(Integer from,Integer to,Double money);
}
/****************************************************/
public class AccountServiceImpl implements AccountService {


    private AccountDao ad;
    public void setAd(AccountDao ad) {
        this.ad = ad;
    }

    @Override
    //转账方法 
    public void transfer( Integer from, Integer to, Double money){
                //减钱
                ad.decreaseMoney(from, money);
                //异常代码
                int i = 1/0;  
                //加钱
                ad.increaseMoney(to, money);
            }

    }

4、开启注解管理事务

<!--  开启注解管理事务 -->
<tx:annotation-driven/>

5、使用注解

在使用事务的类或者方法上加一个注解:@Transactional

    @Override
    //转账方法
    @Transactional(isolation =Isolation.REPEATABLE_READ,propagation= Propagation.REQUIRED,readOnly= false)
    public void transfer( Integer from, Integer to, Double money){
                //减钱
                ad.decreaseMoney(from, money);
                int i = 1/0;
                //加钱
                ad.increaseMoney(to, money);
            }

6、测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Demo2 {

    @Resource(name = "accountService")
    private AccountService as;

    @Test
    public void fun1(){
        as.transfer(1, 2, 100d);

    }

    结果: A和B账户的钱都没有发生变化,事务注解成功!

    上面这种是AspectJ的方式,Aspect 是一套独立的面向切面编程的解决方案。Spring 集成了与Aspect 5 一样的注解,并使用了AspectJ 来做切入点的解析和匹配。

SpringAOP 中选择@AspectJ 还是Schema (XML)?

  • @AspectJ 是静态编译方式,Schema AOP 是动态编译方式
  • XML风格有两个缺点:1、因为需求被分割到了通知类的声明中和xml配置文件中,他不能完全将需求实现的地方封装到一个位置,违背了DRY原则。当使用 @AspectJ 风格时就只有一个独立的模块-切面,信息被封装了;2、xml风格同@Asepect风格所能表达的内容有更多的限制,仅支持“singleton”切面实例模型,并且不能在xml中组合命名连接点的声明,@AspectJ风格支持其他的实例模型以及更丰富的连接点组合。
  • @Aspect 切面能被Spring AOP 和 AspectJ两者都能理解。

总结

    以上介绍了AOP注解方式实现声明式事务,相对于XML方式,代码简洁了,注解方式越来越流行,感谢您的阅读!

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zt15732625878/article/details/81348710
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-06 22:04:52
  • 阅读 ( 1169 )
  • 分类:研发管理

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢