Spring Aspectj Around 建议不适用于接口实现 [英] Spring Aspectj Around advice does not work with interface implementations

查看:19
本文介绍了Spring Aspectj Around 建议不适用于接口实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口及其实现.

I have one interface and its implementations.

interface A{
    String methodA();
    String methodB();
}

public class Impl1 implements A{

    @Override
    public String methodA() {
        methodB();
        return "";
    }

    @Override
    public String methodB() {
        return "";
    }
}

public class Impl2 implements A{

    @Override
    public String methodA() {
        methodB();
        return "";
    }

    @Override
    public String methodB() {
        return null;
    }
}

当A接口的任何实现&&时我想拦截什么执行methodB().

What i want to intercept when any of the implementations of A interface && execution of methodB().

@Around("within(com.bla.bla.A+) && execution(* methodB(..))")

但这没有用.当我删除执行部分时,它可以工作,但用于调用外部方法.任何想法将不胜感激.

But this did not work. When i remove execution part, it works but for calling method of the outside. Any idea would be appreciated.

注意:methodb 不会直接从接口触发.它在接口实现中触发.

Note: methodb is not triggered direcly out of the interface. it triggers in interface implementations.

推荐答案

Spring AOP 适用于代理.从 methodA() 调用 methodB() 称为自调用.Spring AOP 将无法建议从 methodA() 对 methodB() 的方法调用,因为它不会通过代理.

Spring AOP works on proxies. Calling methodB() from methodA() is called a self-invocation. Spring AOP will not be able to advice the method call to methodB() from methodA() , as it will not go through the proxy.

Spring 参考文档:了解 AOP 代理.通读以开头的部分,这里要理解的关键是 main(..) 中的客户端代码

Spring reference documentation : Understanding AOP Proxies . Read through the section starting with The key thing to understand here is that the client code inside the main(..)

这篇关于Spring Aspectj Around 建议不适用于接口实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆