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

查看:189
本文介绍了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.

注意:方法b不会直接从接口之外触发.它会在接口实现中触发.

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参考文档:

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天全站免登陆