Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Value cannot be used as aliased meta-annotation [SPR-13603] #18181

Closed
spring-projects-issues opened this issue Oct 23, 2015 · 6 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 23, 2015

Jose Antonio Iñigo opened SPR-13603 and commented

It would be nice to be able to use @Value as meta annotation:

Having a properties file and the necessary context:properties-placeholder configuration...

someproperty=somevalue

In a Spring bean we could have:

@Value("${someproperty}")
private String someProperty;

Where someProperty's value would be "somevalue".

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".

@Refreshable("${someproperty}")
private String someProperty;

Reference URL: http://stackoverflow.com/questions/32964391/annotation-with-spring-value-meta-annotation-not-injecting-the-value

Issue Links:

Referenced from: commits 4f95593

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

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?

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Jose Antonio Iñigo commented

Hi Juergen, first of all, thank you for your quick anwer. I tried @AliasFor but unfortunately it didn't work.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("")
public @interface Refreshable {
	@AliasFor(annotation=Value.class, attribute="value")
	String value();
}

The value of the fields that use @Refreshable is always "".

Now removing @Value("") and keeping only the @AliasFor I get null:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Refreshable {
	@AliasFor(annotation=Value.class, attribute="value")
	String value();
}

Maybe I didn't understand well how to use @AliasFor...

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

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.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This revision is in master now, allowing for @AliasFor references to @Value. Feel free to give the upcoming 4.3.0.BUILD-SNAPSHOT a try...

Juergen

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Dec 29, 2015

Sam Brannen commented

Thanks, Juergen Hoeller!

@Value somehow 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
Copy link
Collaborator Author

Jose Antonio Iñigo commented

@Juergen I just tested it and works perfectly. Thank you for the quick solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants