Spring 方面调用接口方法上的自定义注释 [英] Spring aspect call on custom annotation on interface method
问题描述
我有这个界面:
公共接口 FakeTemplate {@CustomAnnotation无效的 foo() {}}
还有这个接口的实现:
@Component公共 FakeImpl 实现 FakeTemplate {@覆盖公共无效 foo() {//做东西}}
还有这方面:
@Aspect@成分公共类 CustomAspect {@Before(value = "@annotation(com.fake.CustomAnnotation)")public void doStuffBefore(JoinPoint joinPoint} {}}
我使用 spring 并使用以下方法启用 AspectJ:@EnableAspectJAutoProxy(proxyTargetClass = true)
我的问题是在执行 FakeImpl 的 foo()
方法之前没有调用方面 doStuffBefore
方法.当我将 @CustomAnnotation
放在 FakeImpl
而不是 FakeTemplate
上时它确实有效,但我更喜欢将注释放在 上FakeTemplate
因为它在一个单独的 API 包中,我已经委托它作为我放置所有注释的地方.
我还想确保在实现 FakeTemplate
的每个类上都调用 CustomAnnotation
,而不记得将注释放在所有实现类本身上.>
如果注解只在接口类上,有什么办法可以得到要调用的通知吗?
注解继承不适用于 Java 中的方法.但是您可以使用其他切入点表达式,例如执行(public * FakeTemplate+.foo(..))
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?
Annotation inheritance doesn't work for methods in java.
But you can use other pointcut expression, something like
execution(public * FakeTemplate+.foo(..))
这篇关于Spring 方面调用接口方法上的自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!