Related instructions:
In SpringBoot,We can obtain and bind the information in the configuration file in the following ways:
@Value annotation.
Use Environment.
@ConfigurationProperties annotation.
By implementing the ApplicationListener interface ,registering the listener, for hard-coded acquisition, please refer to:https://www.jb51.net/article/187407. htm
Hard coded loading file acquisition.
……
Note : Under normal circumstances, the first and second methods are enough; but if you want to get them directly from the configuration file As for arrays, lists, maps, and objects, the support for the first and second types is not very good. Currently, only arrays, lists, and maps are available. As for beans, I’m a little helpless.
At this time we can use the third method to obtain.
Environment Description:Windows10, IntelliJ IDEA, SpringBoot 2.1.5.RELEASE
@ConfigurationProperties annotation to obtain configuration information and bind to the object example: ;
Preparation work:Introducing spring-boot-configuration-processor dependency
Give me my complete pom.xml file:
xsi:schemaLocation=”http: //maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.aspire.yaml-properties
yaml-properties
0.0.1-SNAPSHOT
Yaml file and properties file syntax examples
yaml file and properties file syntax examples
1.8
org.springframework.boot
spring-boot-starter
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-configuration-processor
org.springframework.boot
spring-boot-maven-plugin
Load demo. The information in the properties file is bound to the bean, and injected into the container example:
Project structure description:
Note:application.yml is actually in this demonstration, Not used. However, I gave an example of how to perform similar configuration in a yml file (see the end of this article).
Note: If you want to read a file in yml or yaml, then it is best to write it directly in application.yml or
application.yaml If the file is written in other yml files, it may cause reading failure.
(P.S. If it is written in other yml or yaml files, it can only be positioned through the last level key.
Location is possible through multi-level keys. Not enough.)
First give a Uer class that will be used later:
Note: The annotations in the above picture are rapid development lombok annotations. You can also not use these annotations. ,Write getter, setter and other methods yourself.
This is what DemoConfig looks like:
Note:The@Data annotation in the above picture belongs to the rapid development lombok annotation,This annotation is not necessary,It can also be omitted The annotation is handwritten by myself
getter, setter, toString, etc.
This is what is in deme.properties:
Test it:
Run the test method,Console output (for the convenience of observation,I manually wrap the lines and sort out the output content)&# xff1a;
It can be seen that the information in the demo.peoperties file was successfully bound to the bean and injected into the Spring container successfully!
Expansion:
If it is an application.yml (or application.yaml) file ,similar,we can configure it like this:
Come to this article about “ConfigurationProperties binding configuration information to Array, List, This concludes the article on the implementation of Map and Bean. For more information about “ConfigurationProperties bound to Array, List, Map, and Bean”, please search Script House’s previous articles or continue to browse the following related articles. I hope you will do so in the future. Duoduo supports Script House!