Spring aspect call on custom annotation on interface method

a 夏天 提交于 2019-12-25 08:48:38

问题


I have this interface:

public interface FakeTemplate {

    @CustomAnnotation
    void foo() {

    }

}

And this implementation of the interface:

@Component
public FakeImpl implements FakeTemplate {

    @Override
    public void foo() {
        //Do Stuff
    }

}

And this aspect:

@Aspect
@Component
public class CustomAspect {

    @Before(value = "@annotation(com.fake.CustomAnnotation)")
    public void doStuffBefore(JoinPoint joinPoint} {

    }

}

I'm using spring with AspectJ enabled using: @EnableAspectJAutoProxy(proxyTargetClass = true)

My issue is that the aspect doStuffBefore method is not being called before the execution of FakeImpl's foo() method. It Does work when I put the @CustomAnnotation on FakeImpl instead of FakeTemplate, but I'd much prefer to put the annotation on FakeTemplate as it's in a separate API package and I've kind of delegated it as the place where I put all my annotations.

I'd also like to ensure that the CustomAnnotation is called on every class that implements FakeTemplate without remembering to put the annotation on all the implementation classes themselves.

Is there any way to get the advice to be called if the annotation is only on the interface class?


回答1:


Annotation inheritance doesn't work for methods in java. But you can use other pointcut expression, something like execution(public * FakeTemplate+.foo(..))



来源:https://stackoverflow.com/questions/36065654/spring-aspect-call-on-custom-annotation-on-interface-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!