Skip to content

Commit 416cfc2

Browse files
committed
Improve @value documentation
Improve "@ConfigurationProperties vs. @value" documentation with the recommendation that kebab-case is used with the `@Value` annotation. Closes gh-20507
1 parent 291165f commit 416cfc2

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ The following table summarizes the features that are supported by `@Configuratio
13031303

13041304
| <<boot-features-external-config-relaxed-binding,Relaxed binding>>
13051305
| Yes
1306-
| No
1306+
| Limited (see note below)
13071307

13081308
| <<appendix-configuration-metadata.adoc#configuration-metadata,Meta-data support>>
13091309
| Yes
@@ -1315,7 +1315,12 @@ The following table summarizes the features that are supported by `@Configuratio
13151315
|===
13161316

13171317
If you define a set of configuration keys for your own components, we recommend you group them in a POJO annotated with `@ConfigurationProperties`.
1318-
You should also be aware that, since `@Value` does not support relaxed binding, it is not a good candidate if you need to provide the value by using environment variables.
1318+
Doing so will provide you with structured, type-safe object that you can inject into your own beans.
1319+
1320+
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).
1321+
This will allow Spring Boot to use the same logic as it does when relaxed binding `@ConfigurationProperties`.
1322+
For example, `@Value("{demo.item-price}")` will pick up `demo.item-price` and `demo.itemPrice` forms from the `application.properties` file, as well as `DEMO_ITEMPRICE` from the system environment.
1323+
If you used `@Value("{demo.itemPrice}")` instead, `demo.item-price` and `DEMO_ITEMPRICE` would not be considered.
13191324

13201325
Finally, while you can write a `SpEL` expression in `@Value`, such expressions are not processed from <<boot-features-external-config-application-property-files,application property files>>.
13211326

0 commit comments

Comments
 (0)