Skip to content

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

Closed

Description

@spring-projects-issues

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

Activity

  1. spring-projects-issues commented on Oct 23, 2015

    @spring-projects-issues
    CollaboratorAuthor

    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

  2. spring-projects-issues commented on Oct 26, 2015

    @spring-projects-issues
    CollaboratorAuthor

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

  3. spring-projects-issues commented on Oct 26, 2015

    @spring-projects-issues
    CollaboratorAuthor

    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

  4. spring-projects-issues commented on Dec 29, 2015

    @spring-projects-issues
    CollaboratorAuthor

    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

  5. spring-projects-issues commented on Dec 29, 2015

    @spring-projects-issues
    CollaboratorAuthor

    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. ;)

  6. spring-projects-issues commented on Dec 30, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Jose Antonio Iñigo commented

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

  7. added
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions