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

Clearly document the limitations of @Value with respect to relaxed binding #4413

Closed
johanhaleby opened this issue Nov 9, 2015 · 5 comments
Closed
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@johanhaleby
Copy link

Suppose I have a property like this:

@Value("${something.with-dash}")
private String mySomething;

It's hard to make Spring map hyphens like with-dash from an environment variable. Let's say I've built my spring onejar then I can't just provide an environment variable like this (bash):

$ SOMETHING_WITH-DASH=Something java -jar my-spring-onejar.jar

You need to use a work-around like this:

$ env 'SOMETHING_WITH-DASH=Something' java -jar my-spring-onejar.jar

But in certain frameworks such as Kubernetes you cannot use a hyphen in environment variables since they use the C_IDENTIFIER format as the lowest common denominator. I've aded an issue for this in Kubernetes but I'm not sure if they're willing to change it.

So I wonder if instead Spring could enhance its way that hyphens are treated in environment variables when matching the @Value annotation? For example if SOMETHING_WITH_DASH could map to something.with-dash?

@johanhaleby johanhaleby changed the title Convert underscore to hyphen/dash when from environment variables Convert environment variables with underscore to hyphen/dash Nov 9, 2015
@snicoll
Copy link
Member

snicoll commented Nov 9, 2015

Are you aware that we are already supporting this with @ConfigurationProperties? I'd argue that it is far more superior than the basic @Value approach so you might just as well give that a try. @Value is a core framework feature and the relaxed binding is not available there.

@johanhaleby
Copy link
Author

Hmm I think at least some of the relaxed binding properties work for @Value as well. But why shouldn't the same relaxed binding properties work for both @ConfigurationProperties and @Value?

@snicoll
Copy link
Member

snicoll commented Nov 9, 2015

Yes, there are some basic relaxed binding in the core framework as well.

Advanced relaxed binding is a spring boot feature and is quite opinionated so it does not belong in the core framework IMO. @Value is a core framework feature. I think we could improve the core feature (to some extend) but regardless of that I'd strongly advice not to use @Value with Spring Boot as a better solution exists already (see link in my first answer).

WDYT @dsyer @wilkinsona ?

@johanhaleby
Copy link
Author

Ah I see. Perhaps the documentation can be even more clear that the same rules/properties does not apply to the @Value annotation since it's in core? I think that for a casual user like myself it can be a bit confusing to have a different set of rules for relaxed binding. It's sometimes hard to know what's in "core" and what's in "spring boot" since you typically just include a spring-boot-starter-x in your pom/build.gradle file. But I do understand that you want to have a clean separation between core and boot as well.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 17, 2016
@wilkinsona wilkinsona added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 2, 2016
@wilkinsona wilkinsona modified the milestones: 1.4.0.RC1, 1.4.0 Jul 2, 2016
@wilkinsona wilkinsona changed the title Convert environment variables with underscore to hyphen/dash Clearly document the limitations of @Value with respect to relaxed binding Jul 2, 2016
@snicoll snicoll self-assigned this Jul 11, 2016
@snicoll
Copy link
Member

snicoll commented Jul 11, 2016

You need to use a work-around like this:

I've just built a small example with SOMETHING_WITH-DASH and your code above with @Value breaks for me.

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'something.with-dash' in string value "${something.with-dash}"

On a second though and checking the core framework again we don't have any such transformation of the key. I am busy documenting the differences between @Value and @ConfigurationProperties in #4892 anyway

@snicoll snicoll closed this as completed Jul 11, 2016
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed type: documentation A documentation update labels Jul 11, 2016
@snicoll snicoll removed this from the 1.4.0 milestone Jul 11, 2016
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: invalid An issue that we don't feel is valid labels Jul 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants