Spring方面调用接口方法上的自定义注释 [英] Spring aspect call on custom annotation on interface method
问题描述
我有这个界面:
public interface FakeTemplate {
@CustomAnnotation
void foo() {
}
}
接口的这个实现:
@Component
public FakeImpl implements FakeTemplate {
@Override
public void foo() {
//Do Stuff
}
}
这方面:
@Aspect
@Component
public class CustomAspect {
@Before(value = "@annotation(com.fake.CustomAnnotation)")
public void doStuffBefore(JoinPoint joinPoint} {
}
}
我正在使用带有AspectJ的spring启用使用: @EnableAspectJAutoProxy(proxyTargetClass = true)
I'm using spring with AspectJ enabled using: @EnableAspectJAutoProxy(proxyTargetClass = true)
我的问题是方面在执行Fake之前没有调用doStuffBefore
方法Impl的 foo()
方法。当我将 @CustomAnnotation
放在 FakeImpl
而不是 FakeTemplate $ c时,它会起作用$ c>,但我更喜欢将注释放在
FakeTemplate
上,因为它位于一个单独的API包中,我将其委托为我放置的位置我的所有注释。
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.
我还想确保在每个实现 CustomAnnotation
> FakeTemplate ,而不记得将注释放在所有实现类本身上。
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?
推荐答案
注释继承不适用于java中的方法。
但你可以使用其他切入点表达式,例如
执行(public * FakeTemplate + .foo(..))
Annotation inheritance doesn't work for methods in java.
But you can use other pointcut expression, something like
execution(public * FakeTemplate+.foo(..))
这篇关于Spring方面调用接口方法上的自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!