Skip to content

Clarify documentation regarding relaxed binding of environment variables #22974

@Maxeh

Description

@Maxeh

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions