Spring方面调用接口方法上的自定义注释

phi*_*han 5 java spring annotations interface spring-aop

我有这个界面:

public interface FakeTemplate {

    @CustomAnnotation
    void foo() {

    }

}
Run Code Online (Sandbox Code Playgroud)

以及该接口的实现:

@Component
public FakeImpl implements FakeTemplate {

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

}
Run Code Online (Sandbox Code Playgroud)

还有这方面:

@Aspect
@Component
public class CustomAspect {

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

    }

}
Run Code Online (Sandbox Code Playgroud)

我正在使用启用了 AspectJ 的 spring,使用:@EnableAspectJAutoProxy(proxyTargetClass = true)

我的问题是doStuffBefore在执行 FakeImpl 的方法之前没有调用方面方法foo()@CustomAnnotation当我把onFakeImpl而不是 时,它确实可以工作FakeTemplate,但我更喜欢把注释放在 on 上FakeTemplate,因为它位于单独的 API 包中,并且我将它委托为放置所有注释的地方。

我还想确保CustomAnnotation在每个实现的类上调用 ,FakeTemplate而无需记住在所有实现类本身上添加注释。

如果注释仅在接口类上,有什么方法可以获取要调用的建议吗?

Evg*_*eny 5

注解继承不适用于 java 中的方法。但您可以使用其他切入点表达式,例如 execution(public * FakeTemplate+.foo(..))