It seems that there are wrong information in the docs in the section Binding from Environment Variables. A green info section states the following:
Underscores cannot be used to replace the dashes in property names. If you attempt to use SPRING_MAIN_LOG_STARTUP_INFO with the example above, no value will be bound.
After some testing with the newest version of spring boot it turns out that it is actually possible. For example we can not only bind the environment variable TEST_MYPROJECT_MYVALUE
but also TEST_MY_PROJECT_MY_VALUE
to @ConfigurationPropertis
and @Value
like this:
@ConfigurationProperties("test.my-project")
public class MyValueConfig {
private String myValue;
}
@Value("${test.my-project.my-value}")
So this section in the docs should be adjusted I guess. Moreover, in section @ConfigurationProperties vs. @Value I do not understand why Relaxed Binding is marked as "limited" for @Value
. From my testing all environment name combinations could be used for both @ConfigurationProperties
and @Value
. E.g. all of those combinations could be bound to the @ConfigurationProperties
and @Value
shown above:
test.my-project.my-value=hello
test.myProject.my-value=hello
TEST.myProject.my-value=hello
TEST.MY_PROJECT.my-value=hello
TEST.MY-PROJECT.my-value=hello
TEST_MYPROJECT_MYVALUE=hello
TEST_MY_PROJECT_MY_VALUE=hello
I could not find a single combination that was working for @ConfigurationProperties
but not for @Value
.
Comment From: snicoll
Thanks for the report. There has been some improvements in relaxed binding that makes it more ubiquitous and I agree the section you've referenced could use a review.
Comment From: yayayahei
hahahaha, have found the same problem, and I've asked at gitter but got no response. I'm suprised to find this issue.
Comment From: wilkinsona
https://github.com/spring-projects/spring-boot/issues/10873 added support for replacing -
with _
. It was added for compatibility with 1.5 and we don't want to encourage its usage. That said, the current note in the docs is inaccurate so I think we should remove it.
Relaxed binding is working with @Value
as the canonical, kebab-case form is being used:
@Value("${test.my-project.my-value}")
This is covered in the paragraph to which the limited entry in the table refers:
If you do want to use
@Value
, we recommend that you refer to property names using their canonical form (kebab-case using only lowercase letters). This will allow Spring Boot to use the same logic as it does when relaxed binding@ConfigurationProperties
. For example,@Value("{demo.item-price}")
will pick updemo.item-price
anddemo.itemPrice
forms from theapplication.properties
file, as well asDEMO_ITEMPRICE
from the system environment. If you used@Value("{demo.itemPrice}")
instead,demo.item-price
andDEMO_ITEMPRICE
would not be considered.
The table refers to it is a note, but it is a plain paragraph in the documentation. I think we could improve things by turning it into a NOTE:
.