Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

java - Passing annotation properties to meta-annotations

Say I have an annotation with a property:

@Named(name = "Steve")
private Person person

and I want to create a compound annotation with several meta-annotations, including the one that takes a property

@Named
@AnotherAnnotation
@YetAnotherAnnotation
public @interface CompoundAnnotation {

    ...
}

Is there a way that I can pass properties to the compound annotation to one of the meta annotations?

Eg, something like this:

@CompoundAnnotation(name = "Bob")
private Person person;

that is equivalent to, but much more convenient than

@Named(name = "Bob")
@AnotherAnnotation
@YetAnotherAnnotation
private Person person;

Thanks!

PS apologies for my poor choice of an example annotation - I didn't have the javax.inject.@Named annotation in mind, just some arbitrary annotation that has properties.


Thank you everyone for your answers/comments.

It definitely seems to be the case that this is not possible. However, it just happens that there is a simple work-around for my case-in-point, which I will share in case it helps anyone:

I am working with Spring and want to create my own Annotations that have @Component as a meta-annotation, thus being autodetected by component scanning. However, I also wanted to be able to set the BeanName property (corresponding to the value property in @Component) so I could have custom bean names.

Well it turns out that the thoughtful guys at Spring made it possible to do just that - the AnnotationBeanNameGenerator will take the 'value' property of whatever annotation it is passed and use that as the bean name (and of course, by default, it will only get passed annotations that are @Component or have @Component as a meta-annotation). In retrospect this should have been obvious to me from the start - this is how existing annotations with @Component as a meta-annotation, such as @Service and @Registry, can provide bean names.

Hope that is useful to someone. I still think it's a shame that this is not possible more generally though!

question from:https://stackoverflow.com/questions/10087467/passing-annotation-properties-to-meta-annotations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is a few years later now, and since you are using Spring, what you are asking for is sort of possible now using the @AliasFor annotation.

For example:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringApplicationConfiguration
@ActiveProfiles("test")
public @interface SpringContextTest {

    @AliasFor(annotation = SpringApplicationConfiguration.class, attribute = "classes")
    Class<?>[] value() default {};

    @AliasFor("value")
    Class<?>[] classes() default {};
}

Now you can annotate your test with @SpringContextTest(MyConfig.class), and the amazing thing is that it actually works the way you would expect.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...