简体   繁体   English

是否可以编写 AOP 注释?

[英]Is it possible to compose AOP annotations?

So, I have 2 annotation A and B coming from existing java libraries and that implement AOP.因此,我有 2 个注释 A 和 B 来自现有的 Java 库并实现了 AOP。

Both of them do something that is necessary to my class and I need to put them in this order (A before B):他们都做了一些对我的班级来说必不可少的事情,我需要把它们按这个顺序排列(A 在 B 之前):

public class MyClass{

    @A(parameter="par")
    @B(value="key")
    public void method(){

    }
}

Would it be possible to define a 3rd annotation, C, as a composition of both?是否可以将第三个注释 C 定义为两者的组合? So in the end I would get:所以最后我会得到:

public class MyClass{

    @C(parameter="par", value="key")
    public void method(){

    }
}

edit: added the requirement of taking a parameter from @A as well and the fact that I don't have the source code as the annotations are taken from libs.编辑:添加了从@A 获取参数的要求以及我没有源代码的事实,因为注释是从库中获取的。

It depends on library you use.这取决于您使用的库。 for example spring does例如春天做

 @Documented
   @Inherited
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(excludeFilters = {
            @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
            @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
    public @interface SpringBootApplication {

        /**
         * Exclude specific auto-configuration classes such that they will never be applied.
         * @return the classes to exclude
         */
        @AliasFor(annotation = EnableAutoConfiguration.class)
        Class<?>[] exclude() default {};

with this approach you would have:使用这种方法,您将拥有:

    @A
    @B
    public @interface C{
        @AliasFor(annotation = B.class)
        String value();
        @AliasFor(annotation = А.class, attribute = "parameter")
        String param();
    }
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@A
@B
public @interface C {
    @AliasFor(annotation = B.class, attribute = "value")
    String value() default {};
}

Let me know if this works.让我知道这个是否奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM