In the previous version of SpringBoot, the @ConfigurationProperties between the configuration file and the class are bound to the corresponding configuration values through Setter methods. Constructor binding has been supported since version 2.2.

Blog: blog.yuqiyu.com/springboot-…

@ ConstructorBinding annotations

This annotation was added by SpringBoot in the 2.2 release, and the property configuration class that adds this annotation no longer needs to add Setter methods, but instead needs to add constructors that instantiate the property configuration class.

Create a project

Create a SpringBoot project with IDEA and add the dependency to pom.xml as follows:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>Copy the code

Configuration information

This chapter mainly explains how to automatically map the contents of the application.yml or application.properties configuration file to the corresponding property field of the configuration class, so we need to add some of our custom configuration content to the application.yml file, as follows:

# Custom configuration minbox: config: author: Heng Yu-you-Yu Qi-yu blog-address: http://blog.yuqiyu.comCopy the code

The configuration class

We write a mapping configuration class named MinBoxConfig corresponding to the configuration information of application.yml, as follows:

/** * @author */ @configurationProperties (prefix = prefix) @constructorBinding Public class MinBoxConfig { /** * public static final String PREFIX = "minbox.config"; /** * private String author; /** * private String blogAddress; public MinBoxConfig(String author, String blogAddress) { this.author = author; this.blogAddress = blogAddress; } public String getAuthor() { return author; } public String getBlogAddress() { return blogAddress; }}Copy the code

In previous versions we used @Configuration and @ConfigurationProperties for Configuration mapping. Since SpringBoot2.1.1. RELEASE we don’t need @Configuration anymore. As long as combining @ ConfigurationPropertiesScan @ ConfigurationProperties tie-in use can, will automatically scan properties under the specified package for binding configuration class.

Add the @constructorBinding annotation to the property configuration class to set the corresponding field values as constructors. We only need to define the parameters of the binding assignment as constructors.

In the above code, the MinBoxConfig configuration class constructor has two parameters: Author and blogAddress, so when instantiating the MinBoxConfig object, only these two configurations are assigned from the corresponding application.yml.

Run the test

A test class annotated by @SpringbooTtest is automatically created in SRC /test/ Java /{packages} to verify that the configuration has been assigned to the configuration class, as shown below:

@SpringBootTest class SpringbootConstructorBindingPropertiesApplicationTests { @Autowired private MinBoxConfig minBoxConfig; @test void printConfig() {system.out.println (" author name: "+ minboxconfig.getauthor ()); System. The out. Println (" the author blog: "+ minBoxConfig. GetBlogAddress ()); }}Copy the code

After running the printConfig() method, the output looks like this:

Author name: Heng Yushaian-Yu Qiyu Author blog address: http://blog.yuqiyu.comCopy the code

Type on the blackboard and underline

The @ConfigurationProperties annotation allows the contents of our configuration files to be mapped directly to Java configuration classes and automatically registered with IOC through scanning, making it much easier to use configuration content in projects.

Code sample

If you like this article please click Star for source repository, thanks!! The sample source code for this article can be obtained from the directory springboot2. x/springboot-constructor-binding-properties:

  • Gitee:Gitee.com/hengboy/spr…

Author’s Personal blog

Use the open source framework ApiBoot to help you become an Api service architect