Skip to content

Commit f0ec571

Browse files
committed
Document relaxed binding from the environment
Update the reference documentation with more details about how relaxed binding works against environment variables. Closes gh-18215
1 parent 359b508 commit f0ec571

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ You can provide default values for your application in `application.properties`
504504
These default values can then be overridden at runtime with a different file located in one of the custom locations.
505505

506506
NOTE: If you use environment variables rather than system properties, most operating systems disallow period-separated key names, but you can use underscores instead (for example, `SPRING_CONFIG_NAME` instead of `spring.config.name`).
507+
See <<boot-features-external-config-relaxed-binding-from-environment-variables>> for details.
507508

508509
NOTE: If your application runs in a container, then JNDI properties (in `java:comp/env`) or servlet context initialization parameters can be used instead of, or as well as, environment variables or system properties.
509510

@@ -978,9 +979,8 @@ NOTE: The `prefix` value for the annotation _must_ be in kebab case (lowercase a
978979
| Standard YAML list syntax or comma-separated values
979980

980981
| Environment Variables
981-
| Upper case format with underscore as the delimiter.
982-
`_` should not be used within a property name
983-
| Numeric values surrounded by underscores, such as `MY_ACME_1_OTHER = my.acme[1].other`
982+
| Upper case format with underscore as the delimiter (see <<boot-features-external-config-relaxed-binding-from-environment-variables>>).
983+
| Numeric values surrounded by underscores (see <<boot-features-external-config-relaxed-binding-from-environment-variables>>)`
984984

985985
| System properties
986986
| Camel case, kebab case, or underscore notation
@@ -989,6 +989,10 @@ NOTE: The `prefix` value for the annotation _must_ be in kebab case (lowercase a
989989

990990
TIP: We recommend that, when possible, properties are stored in lower-case kebab format, such as `my.property-name=acme`.
991991

992+
993+
994+
[[boot-features-external-config-relaxed-binding-maps]]
995+
===== Binding Maps
992996
When binding to `Map` properties, if the `key` contains anything other than lowercase alpha-numeric characters or `-`, you need to use the bracket notation so that the original value is preserved.
993997
If the key is not surrounded by `[]`, any characters that are not alpha-numeric or `-` are removed.
994998
For example, consider binding the following properties to a `Map`:
@@ -1008,6 +1012,32 @@ The properties above will bind to a `Map` with `/key1`, `/key2` and `key3` as th
10081012
NOTE: For YAML files, the brackets need to be surrounded by quotes for the keys to be parsed properly.
10091013

10101014

1015+
[[boot-features-external-config-relaxed-binding-from-environment-variables]]
1016+
===== Binding from Environment Variables
1017+
Most operating systems impose strict rules around the names that can be used for environment variables.
1018+
For example, Linux shell variables can contain only letters (`a` to `z` or `A` to `Z`), numbers (`0` to `9`) or the underscore character (`_`).
1019+
By convention, Unix shell variables will also have their names in UPPERCASE.
1020+
1021+
Spring Boot's relaxed binding rules are, as much as possible, designed to be compatible with these naming restrictions.
1022+
1023+
To convert a property name in the canonical-form to an environment variable name you can follow these rules:
1024+
1025+
* Replace dots (`.`) with underscores (`_`).
1026+
* Remove any dashes (`-`).
1027+
* Convert to uppercase.
1028+
1029+
For example, the configuration property `spring.main.log-startup-info` would be an environment variable named `SPRING_MAIN_LOGSTARTUPINFO`.
1030+
1031+
NOTE: Underscores cannot be used to replace the dashes in property names.
1032+
If you attempt to use `SPRING_MAIN_LOG_STARTUP_INFO` with the example above, no value will be bound.
1033+
1034+
Environment variables can also be used when binding to object lists.
1035+
To bind to a `List`, the element number should be surrounded with underscores in the variable name.
1036+
1037+
For example, the configuration property `my.acme[0].other` would use an environment variable named `MY_ACME_0_OTHER`.
1038+
1039+
1040+
10111041
[[boot-features-external-config-complex-type-merge]]
10121042
==== Merging Complex Types
10131043
When lists are configured in more than one place, overriding works by replacing the entire list.

0 commit comments

Comments
 (0)