如何解决Spring在同类方法相互调用中,事务,缓存等注解不生效的问题

在Spring开发中,同类内部方法调用可能导致事务和缓存失效。文章介绍了三种解决方法:1) 将事务和缓存方法抽取到单独的服务;2) 自动注入自身并调用代理对象;3) 使用Spring工具类获取Bean的代理对象进行调用。这三种方法旨在确保事务和缓存正常生效。
摘要由CSDN通过智能技术生成

☀️相信在日常开发中,一定经历过这样一个场景,就以下面这段代码抽象一下:

@Service
public class ServiceA {
    
    public void methodA() {
        methodB();
        methodC();
    }

    @Transactional
    public void methodB() {
        // biz..
    }

    @Cacheable
    public void methodC() {
        // biz..
    }

}

当我们在使用 serviceA.methodA() 的时候,会发现,methodB的事务不生效,methodC的缓存不生效,这就是 Spring 的事务,缓存不生效的经典场景,即同类中的方法调用不生效,而产生这种情况的原因是因为,调用的是同类中的方法,而不是被代理过的方法,因为调用的不是代理的方法,所以不走事务以及缓存的逻辑。

⚡️解決方法1:
既然是同类中的方法引起的,那么就将事务和缓存方法抽离,放到另外一个bean里面进行,这也是较麻烦的。

@Service
public class ServiceA {
    @Autowired
    private ServiceB serviceB;

    public void methodA() {
        serviceB.methodB();
        serviceB.methodC();
    }
}

@Service
public class ServiceB {

    @Transactional
    public void methodB() {
        // biz..
    }

    @Cacheable
    public void methodC() {
        // biz..
    }
}

⚡️解决方法2:
既然是没走代理的方法,那么让他调用代理的对象的代理方法即可

@Service
public class ServiceA {

    @Autowired
    private ServiceA serviceA;

    public void methodA() {
        serviceA.methodB();
        serviceA.methodC();
    }

    @Transactional
    public void methodB() {
        // biz..
    }

    @Cacheable
    public void methodC() {
        // biz..
    }
}

⚡️解决方法3:
通过一些Spring对象的容器工具,一般是已经被封装过的,通过静态方法来获取Bean对象(绕过了自己调用自己),再通过Bean对象来直接调用方法,这样就会走代理,所以这也是比较优雅的。

@Service
public class ServiceA {

    public void methodA() {
        SpringUtil.getBean(ServiceA.class).methodB();
        SpringUtil.getBean(ServiceA.class).methodC();
    }

    @Transactional
    public void methodB() {
        // biz..
    }

    @Cacheable
    public void methodC() {
        // biz..
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值