external-config.adoc (spring-boot-2.7.3) | : | external-config.adoc (spring-boot-2.7.4) | ||
---|---|---|---|---|
skipping to change at line 102 | skipping to change at line 102 | |||
[[features.external-config.files]] | [[features.external-config.files]] | |||
=== External Application Properties [[features.external-config.files]] | === External Application Properties [[features.external-config.files]] | |||
Spring Boot will automatically find and load `application.properties` and `appli cation.yaml` files from the following locations when your application starts: | Spring Boot will automatically find and load `application.properties` and `appli cation.yaml` files from the following locations when your application starts: | |||
. From the classpath | . From the classpath | |||
.. The classpath root | .. The classpath root | |||
.. The classpath `/config` package | .. The classpath `/config` package | |||
. From the current directory | . From the current directory | |||
.. The current directory | .. The current directory | |||
.. The `/config` subdirectory in the current directory | .. The `config/` subdirectory in the current directory | |||
.. Immediate child directories of the `/config` subdirectory | .. Immediate child directories of the `config/` subdirectory | |||
The list is ordered by precedence (with values from lower items overriding earli er ones). | The list is ordered by precedence (with values from lower items overriding earli er ones). | |||
Documents from the loaded files are added as `PropertySources` to the Spring `En vironment`. | Documents from the loaded files are added as `PropertySources` to the Spring `En vironment`. | |||
If you do not like `application` as the configuration file name, you can switch to another file name by specifying a configprop:spring.config.name[] environment property. | If you do not like `application` as the configuration file name, you can switch to another file name by specifying a configprop:spring.config.name[] environment property. | |||
For example, to look for `myproject.properties` and `myproject.yaml` files you c an run your application as follows: | For example, to look for `myproject.properties` and `myproject.yaml` files you c an run your application as follows: | |||
[source,shell,indent=0,subs="verbatim"] | [source,shell,indent=0,subs="verbatim"] | |||
---- | ---- | |||
$ java -jar myproject.jar --spring.config.name=myproject | $ java -jar myproject.jar --spring.config.name=myproject | |||
skipping to change at line 689 | skipping to change at line 689 | |||
This means that the binder will expect to find a constructor with the parameters that you wish to have bound. | This means that the binder will expect to find a constructor with the parameters that you wish to have bound. | |||
If you are using Java 16 or later, constructor binding can be used with records. | If you are using Java 16 or later, constructor binding can be used with records. | |||
In this case, unless your record has multiple constructors, there is no need to use `@ConstructorBinding`. | In this case, unless your record has multiple constructors, there is no need to use `@ConstructorBinding`. | |||
Nested members of a `@ConstructorBinding` class (such as `Security` in the examp le above) will also be bound through their constructor. | Nested members of a `@ConstructorBinding` class (such as `Security` in the examp le above) will also be bound through their constructor. | |||
Default values can be specified using `@DefaultValue` on a constructor parameter or, when using Java 16 or later, a record component. | Default values can be specified using `@DefaultValue` on a constructor parameter or, when using Java 16 or later, a record component. | |||
The conversion service will be applied to coerce the `String` value to the targe t type of a missing property. | The conversion service will be applied to coerce the `String` value to the targe t type of a missing property. | |||
Referring to the previous example, if no properties are bound to `Security`, the `MyProperties` instance will contain a `null` value for `security`. | Referring to the previous example, if no properties are bound to `Security`, the `MyProperties` instance will contain a `null` value for `security`. | |||
If you wish you return a non-null instance of `Security` even when no properties are bound to it, you can use an empty `@DefaultValue` annotation to do so: | To make it contain a non-null instance of `Security` even when no properties are bound to it (when using Kotlin, this will require the `username` and `password` parameters of `Security` to be declared as nullable as they do not have default values), use an empty `@DefaultValue` annotation: | |||
include::code:nonnull/MyProperties[tag=*] | include::code:nonnull/MyProperties[tag=*] | |||
NOTE: To use constructor binding the class must be enabled using `@EnableConfigu rationProperties` or configuration property scanning. | NOTE: To use constructor binding the class must be enabled using `@EnableConfigu rationProperties` or configuration property scanning. | |||
You cannot use constructor binding with beans that are created by the regular Sp ring mechanisms (for example `@Component` beans, beans created by using `@Bean` methods or beans loaded by using `@Import`) | You cannot use constructor binding with beans that are created by the regular Sp ring mechanisms (for example `@Component` beans, beans created by using `@Bean` methods or beans loaded by using `@Import`) | |||
TIP: If you have more than one constructor for your class you can also use `@Con structorBinding` directly on the constructor that should be bound. | TIP: If you have more than one constructor for your class you can also use `@Con structorBinding` directly on the constructor that should be bound. | |||
NOTE: The use of `java.util.Optional` with `@ConfigurationProperties` is not rec ommended as it is primarily intended for use as a return type. | NOTE: The use of `java.util.Optional` with `@ConfigurationProperties` is not rec ommended as it is primarily intended for use as a return type. | |||
As such, it is not well-suited to configuration property injection. | As such, it is not well-suited to configuration property injection. | |||
End of changes. 2 change blocks. | ||||
3 lines changed or deleted | 3 lines changed or added |