springboot2 uses configuration mapping @ConfigurationProperties

Foreword:

        The general idea is to create an object and put the values ​​of the configuration file in the properties of the object.

In this way, there are two steps. First, formulate mapping rules, and then create new objects to fill in attributes.

 

(0) Configuration file

 

(1) Formulate mapping rules

Create a new class and add the @ConfigurationProperties() annotation. In this way, the boot mapping rule is used: mapping by name equality.

@Data is added to the picture to automatically generate get and set methods, and the boot stipulates that the set method must have.

 

1. The names do not need to be equal, the camel case and underscore can be distinguished from each other.

2. @ConfigurationProperties annotation has several values ​​that can be filled:

@ConfigurationProperties(
        ignoreInvalidFields = false,   // 是否无视掉,解析出错的配置文件字段。不无视会怎么样?报错
        ignoreUnknownFields = true,    // 是否无视掉,class文件里少写的字段
        prefix = "my.conf"             // 变量前缀 或者说 命名空间
) 

prefix is ​​the default option. Although it is not written in the figure, prefix is ​​used

 

(2) Generate an instance object of the class and fill in the attribute value

The generated objects in spring can only be annotated with @Component, @Configuration or @Service.

In addition, when the object is generated by him, it will automatically fill in the value. (Because of the annotation @ConfigurationProperties)

 

 

That's it. What you get from spring is an example of filling in the value.

 

 

springboot2 code, maven project

Lanzous Cloud: https://wws.lanzous.com/ib3hnjvnjre

 

 

 

Attached:

Import another configuration file from the configuration file, you can write like this

 

You can also use the @Value annotation to get the configured value

https://www.cnblogs.com/javastack/archive/2020/10/23/13862164.html

 

Code springboot2, maven project

Lanzous Cloud: https://wws.lanzous.com/i2Uzdjycj5e

 

 

 

another:

After writing @ConfigurationProperties, sometimes there will be a notice on the picture

 

First of all, it doesn't matter whether he is. This shows that this is an optional configuration.

 

The specific solution is to add processor dependency in the pom file

https://blog.csdn.net/qq_44173974/article/details/107729850?utm_medium=distribute.pc_relevant_t0.none-task-blog-OPENSEARCH-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-OPENSEARCH-1.control

 

So what is the role of this optional configuration?

- (1) Hope that there can be a "smart reminder" in the configuration file, as shown in the picture

- (2) I don't know if the variable name has been changed, I want to know if it is written correctly (this ip2 is also highlighted)

- If it is correct, you can see some information

Guess you like

Origin blog.csdn.net/u013595395/article/details/111999980