Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing annotation properties to meta-annotations
    primarykey
    data
    text
    <p>Say I have an annotation with a property:</p> <pre><code>@Named(name = "Steve") private Person person </code></pre> <p>and I want to create a compound annotation with several meta-annotations, including the one that takes a property</p> <pre><code>@Named @AnotherAnnotation @YetAnotherAnnotation public @interface CompoundAnnotation { ... } </code></pre> <p>Is there a way that I can pass properties to the compound annotation to one of the meta annotations?</p> <p>Eg, something like this:</p> <pre><code>@CompoundAnnotation(name = "Bob") private Person person; </code></pre> <p>that is equivalent to, but much more convenient than</p> <pre><code>@Named(name = "Bob") @AnotherAnnotation @YetAnotherAnnotation private Person person; </code></pre> <p>Thanks!</p> <p>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.</p> <hr> <p>Thank you everyone for your answers/comments.</p> <p>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:</p> <p>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.</p> <p>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.</p> <p>Hope that is useful to someone. I still think it's a shame that this is not possible more generally though!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload