Spring AOP对嵌套方法不起作用的解决

来自:网络
时间:2022-01-03
阅读:
目录

Spring AOP对嵌套方法不起作用

今天在调研系统操作记录日志时,好多教程都是借助于Spring AOP机制来实现。于是也采用这种方法来实现。在Service中的删除日志方法上注解自定义的切点,但是执行没有生效。

代码如下:

//尝试删除溢出日志
    public synchronized void tryDelOverflowLog() {
        logNum++;
        if (logNum - LogConst.MAX_NUM > 0) {
            int delNum = logNum - LogConst.MAX_NUM + LogConst.EXTRA_NUM;
            logNum -= delNum;
            removeOverflowLog(delNum);
        }
    }
 
    //日志溢出后,删除最新入库的日志
    @ServiceLog(type = LogConst.TYPE_LOG_RECORD, description = "操作日志缓存区溢出,系统自动清空缓存区")
    public void removeOverflowLog(int delNum) {
        custLogMapper.removeOverflowLog(delNum);
    }

在使用 Spring AOP 的时候,我们从 IOC 容器中获取的 Service Bean 对象其实都是代理对象,而不是那些 Service Bean 对象本身,也就是说获取的并不是被代理对象或代理目标。当我在自己的 Service 类中使用 this 关键字嵌套调用同类中的其他方法时,由于 this 关键字引用的并不是该 Service Bean 对象的代理对象,而是其本身,故 Spring AOP 是不能拦截到这些被嵌套调用的方法的。

要解决这个问题

最简单的方法是把自身注入到自身,用注入的这个自身去调用本方法。或者你也可以不用spring aop而是用aspectj weaving,倒是可以测底的解决该问题。我采用的是把自身注入到自身中。

    /**
     * 通过注入自身解决,Spring AOP嵌套调用不生效的问题
     */
    @Autowired
    private ApplicationContext applicationContext;
    private LogService self;
    @PostConstruct
    private void init() {
        self = (LogService) applicationContext.getBean("logService");
    }
   //尝试删除溢出日志
    public synchronized void tryDelOverflowLog() {
        logNum++;
        if (logNum - LogConst.MAX_NUM > 0) {
            int delNum = logNum - LogConst.MAX_NUM + LogConst.EXTRA_NUM;
            logNum -= delNum;
            self.removeOverflowLog(delNum);
        }
    }

Spring AOP、嵌套调用失效及解决

加入注解

@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)

获取当前代理的接口

public interface ICurrentAopProxyService<T> {
    default T getCurrentProxyService() {
        return (T) AopContext.currentProxy();
    }
}

需要嵌套调用的Service实现它

Spring AOP对嵌套方法不起作用的解决

调用的时候改写代码

    public SysMerchantVersion selectByMerchantId(Long merchantId) {
        return getCurrentProxyService().getOne(new QueryWrapper<SysMerchantVersion>()
                .lambda()
                .eq(SysMerchantVersion::getMerchantId, merchantId));
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

返回顶部
顶部