java - Spring方面调用接口(interface)方法上的自定义注释

标签 java spring interface annotations spring-aop

我有这个界面:

public interface FakeTemplate {

    @CustomAnnotation
    void foo() {

    }

}

该接口(interface)的实现:

@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} {

    }

}

我使用 spring 并启用了 AspectJ:@EnableAspectJAutoProxy(proxyTargetClass = true)

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

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

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

最佳答案

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

关于java - Spring方面调用接口(interface)方法上的自定义注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36065654/

相关文章:

java - 不明白为什么这行不通 |循环不会运行

在 Google App Engine 中显示图像的 Java 代码

function - Kotlin:覆盖(并指定)通用接口(interface)函数

java - 在第二个数据源关闭的情况下运行 Spring Boot 应用程序

cocoa - 在 Cocoa 中,将私有(private) API 放在 .m 文件中,将公共(public) API 放在 .h 文件中,这是一个好习惯吗?

java - OOP 设计 : Interface-Methods with implementation-dependent parameters

java - Eclipse:如何禁用条件的换行和缩进

java - 在javascript下使用表达式语言?

spring - MockMvc 和 WebTestClient 有什么区别?

java - Struts 排除带有 Spring 的图案