You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@Value cannot be used as aliased meta-annotation [SPR-13603] #18181
I want to create a new annotation @Refreshable with @Value as meta-annotation so that it behaves the same as using @Value directly. Since @Value requires a value I hardcode "default" hoping that @Refreshable's value() will override it:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("default")
public @interface Refreshable {
String value();
}
Then when I use the new annotation in a bean, I was expecting it to work properly, injecting the declared value "somevalue", but it doesn't, I get "default".
I'm afraid you're running into a limitation of our name-based attribute overriding here: You can't override value attributes, since those are not reliably meant to override a specific meta-annotation's value attribute.
That said, as of Spring Framework 4.2, you can declare an explicit AliasFor annotation that should point to Value.class in your case, indicating that you intend to override that annotation's value attribute. Any other meta-annotation's value attribute will remain untouched. Does this work in your scenario?
Checking the lookup algorithm there, we do not evaluate @AliasFor for @Value processing at all. So I'm afraid this is simply not supported at the moment; I was mistaken that we took it that far already. Anyway, since this really should be working, I'll put this onto the 4.3 roadmap, making sure that QualifierAnnotationAutowireCandidateResolver participates in the sophisticated merging of attributes that 4.2's AnnotatedElementUtils can do now.
Activity
spring-projects-issues commentedon Oct 23, 2015
Juergen Hoeller commented
I'm afraid you're running into a limitation of our name-based attribute overriding here: You can't override
valueattributes, since those are not reliably meant to override a specific meta-annotation'svalueattribute.That said, as of Spring Framework 4.2, you can declare an explicit
AliasForannotation that should point toValue.classin your case, indicating that you intend to override that annotation'svalueattribute. Any other meta-annotation'svalueattribute will remain untouched. Does this work in your scenario?Juergen
spring-projects-issues commentedon Oct 26, 2015
Jose Antonio Iñigo commented
Hi Juergen, first of all, thank you for your quick anwer. I tried
@AliasForbut unfortunately it didn't work.The value of the fields that use
@Refreshableis always "".Now removing
@Value("") and keeping only the@AliasForI get null:Maybe I didn't understand well how to use
@AliasFor...spring-projects-issues commentedon Oct 26, 2015
Juergen Hoeller commented
Checking the lookup algorithm there, we do not evaluate
@AliasForfor@Valueprocessing at all. So I'm afraid this is simply not supported at the moment; I was mistaken that we took it that far already. Anyway, since this really should be working, I'll put this onto the 4.3 roadmap, making sure thatQualifierAnnotationAutowireCandidateResolverparticipates in the sophisticated merging of attributes that 4.2'sAnnotatedElementUtilscan do now.Juergen
spring-projects-issues commentedon Dec 29, 2015
Juergen Hoeller commented
This revision is in master now, allowing for
@AliasForreferences to@Value. Feel free to give the upcoming4.3.0.BUILD-SNAPSHOTa try...Juergen
spring-projects-issues commentedon Dec 29, 2015
Sam Brannen commented
Thanks, Juergen Hoeller!
@Valuesomehow didn't make it into any of the candidate lists in #16020. I guess I just overlooked it... not sure how... but I obviously did. ;)spring-projects-issues commentedon Dec 30, 2015
Jose Antonio Iñigo commented
@JuergenI just tested it and works perfectly. Thank you for the quick solution!