Full Resolution of Configuration Binding for New Features of SpringBoot 2.0

  • 2021-07-09 08:17:41
  • OfStack

Relaxed Binding 2.0 is introduced in Spring Boot 2.0, which makes many improvements to the original attribute binding function to help us load and read configuration information in Spring applications more easily. The following article covers the configuration improvements in Spring Boot 2.0.

Configuration file binding

Simple type

Loading configuration properties in Spring Boot 2.0 will match and load configurations in all lowercase, in addition to removing special characters as in the 1. x version. Therefore, the following four configurations are equivalent:

properties format:


spring.jpa.databaseplatform=mysql
spring.jpa.database-platform=mysql
spring.jpa.databasePlatform=mysql
spring.JPA.database_platform=mysql

yaml format:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql

Tips: Recommended all-lowercase with-delimiter configuration, for example: spring. jpa. database-platform=mysql

List type

Use [] in the properties file to locate list types, such as:


spring.my-example.url[0]=http://example.com
spring.my-example.url[1]=http://spring.io

Comma-split configuration is also supported, and the above configuration is equivalent to the following configuration:


spring.my-example.url=http://example.com,http://spring.io

The following configuration can be used in yaml files:


spring:
 my-example:
  url:
   - http://example.com
   - http://spring.io

Comma splitting is also supported:


spring:
 my-example:
  url: http://example.com, http://spring.io

Note: Configuration for List type in Spring Boot 2.0 must be continuous, or a UnboundConfigurationPropertiesException Exception, so the following configuration is not allowed:


foo[0]=a
foo[2]=b

The above configuration is possible in Spring Boot 1. x, foo[1] Because it is not configured, its value will be null

Map type

The standard configuration of the Map type in properties and yaml is as follows:

properties format:


spring.my-example.foo=bar
spring.my-example.hello=world

yaml Format:


spring:
 my-example:
  foo: bar
  hello: world

Note: If key of Map type contains non-alphanumeric and-characters, it needs to be enclosed by [], such as:


spring:
 my-example:
  '[foo.baz]': bar

Environment property binding

Simple type

Configuration of the environment variable SPRING_JPA_DATABASEPLATFORM=mysql will have the same effect as setting spring. jpa. databaseplatform=mysql1 in the configuration file.

List type

Because [and] symbols cannot be used in environment variables, _ is used instead. Any number surrounded by an underscore will be considered as an array of []. For example:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql
0

In addition, if the last environment variable ends with a number and an underline, the last underline can be omitted. For example, items 1 and 3 in the above example are equivalent to the following configuration:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql
1

System property binding

Simple type

The system properties are similar to those in the file configuration, which are bound by removing special characters and converting them to lowercase. For example, the following command line parameters will have the effect of configuring spring. jpa. databaseplatform=mysql:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql
2

List Type

The binding of system attributes is similar to the binding of file attributes, which is marked by [], such as:


-D"spring.my-example.url[0]=http://example.com"
-D"spring.my-example.url[1]=http://spring.io"

Similarly, he also supports comma separation, such as:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql
4

Attribute reading

Having described the binding of attributes in Spring Boot 2.0 above, we can see that we can have many different expressions for one attribute, but if we want to read attributes in environment of Spring application, the only one name of each attribute conforms to the following rules:

Separate the elements by The last one. Separate the prefix from the attribute name Must be letters (a-z) and numbers (0-9) Must be a lowercase letter Separate words with a hyphen- The only other characters allowed by 1 are [and], which are used for the index of List You can't start with a number

So, if we want to read the configuration of spring. jpa. database-platform in the configuration file, we can write this:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql
5

spring. jpa. database-platform configuration content cannot be obtained in the following way:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql
6

Note: This feature is also required when using @ Value to get configuration content

New binding API

A new binding, API, has been added to Spring Boot 2.0 to help us get configuration information more easily. Here's an example to help you understand it more easily:

Example 1: Simple type

Suppose there is one configuration in the propertes configuration: com. didispace. foo=bar

We create the corresponding configuration class for it:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql
7

Next, the configuration information can be obtained through the latest Binder:


@SpringBootApplication
public class Application {

  public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(Application.class, args);

    Binder binder = Binder.get(context.getEnvironment());

    //  Bind simple configuration 
    FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get();
    System.out.println(foo.getFoo());
  }
}

Example 2: List type

What if the configuration content is List type? For example:


spring:
 jpa:
  databaseplatform: mysql
  database-platform: mysql
  databasePlatform: mysql
  database_platform: mysql
9

Getting these configurations is still very simple, which can be achieved as follows:


ApplicationContext context = SpringApplication.run(Application.class, args);

Binder binder = Binder.get(context.getEnvironment());

//  Binding List Configure 
List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();
System.out.println(post);

List<PostInfo> posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get();
System.out.println(posts);

Code Sample

Examples of this article can be found in the Chapter2-2-1 directory in the following repository:

Github: https://github.com/dyc87112/SpringBoot-Learning
Gitee: https://gitee.com/didispace/SpringBoot-Learning


Related articles: