1024programmer Java Implementation of placing arrays in java configuration files_@ConfigurationProperties binding configuration information to Array, List, Map, and Bean…

Implementation of placing arrays in java configuration files_@ConfigurationProperties binding configuration information to Array, List, Map, and Bean…

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&#xff1a ;

Preparation work:Introducing spring-boot-configuration-processor dependency

a522a25b31600b3c814282f1e7051714.png

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:

b586843a39fc70c6f7598adfefd17550.png

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:

6505b9cac245209bc33f4785780c64e6.png

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:

7417e7a39f9bcb8ba894554dd960bd6f.png

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:

b00cfe957835e492b6a39158d291fd7f.png

Test it:

7d0a3e9a9bc161cd28fa1b4d73ec5eb0.png

Run the test method,Console output (for the convenience of observation,I manually wrap the lines and sort out the output content)&# xff1a;

5851b92e4bc925982f5f37d8926c3871.png

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:

a250789fb48d646f5d3baee05e5e03dc.png

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!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/implementation-of-placing-arrays-in-java-configuration-files_configurationproperties-binding-configuration-information-to-array-list-map-and-bean-3/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索