(learning notes) priority and value of SpringBoot2 configuration file

Priority of profile

For springboot, three configuration files are provided to operate its configuration. yaml is officially recommended, but the default is properties, which is in the priority,

properties > yml > yaml

Basic syntax of yaml

  • Case sensitive.
  • The data value must be preceded by a space as a separator.
  • Use indents to indicate hierarchical relationships.
  • The tab key is not allowed for indenting (except the tab of idea). Only the spacebar is allowed (the number of spaces may be different for each operating system, resulting in hierarchy confusion.)
  • The number of indented spaces is not important, as long as the elements at the same level are aligned to the left.
  • #Indicates a comment.

yaml syntax:

#Two ways to write objects.
person:
  name: zhangsan
  age: 18
person2: {name: zhangsan, age: 18 }
#Two ways to write arrays
hobby:
  - chang
  - tiao
  - rap
  - lanqiu
hobby2: [chang,tiao,rap,lanqiu]
#Pure quantity
msg1 : 'hello \n world!' #Do not recognize pass through characters \ n, output as is
msg2 : "hello \n world!" #Recognize escape characters.

Parameter reference of yaml

name: abc
person:
  name: ${name}
  age: 18

Read the contents of the configuration

There are three ways to read the configuration of the configuration file we write in SpringBoot,

@Value
Environment
@ConfigurationProperties

Next, three different methods of reading are demonstrated.

@Value

First, write a HelloController. For the value of the configuration file, you can use ${} to get the corresponding value. For configurations with few values, this method can be used directly.

@RestController
public class HelloController {
    //Gets the name of the object
    @Value("${person.name}")
    private String name;

    //Take the value of the array,
    @Value("${hobby[0]}")
    private String hobby;

    //    Take the value of pure quantity
    @Value("${msg1}")
    private String msg;

    @RequestMapping("/hello")
    public String hello() {
        return "hello spring boot2!" + "\n" + name + "\n" + hobby + "\n" + msg;
    }

}

Environment

For the Value of this method, we only need to inject * * org. Into the container springframework. core. env. Environment;** Compared with the Value of @ Value, the Value of this method only needs to be obtained through getProperty("key of configuration file"), which is less in ${} format.

    @Autowired
    private Environment env;

    //    The second method takes value through Environment
    @RequestMapping("/world")
    public String world() {
        String env_name = env.getProperty("person.name");
        String env_msg1 = env.getProperty("msg2");
        return "world!"+"\n"+env_name+"\n"+env_msg1;
    }

Compared with Value, this method can be understood as injecting into the container and taking what you want, while @ Value is taking what you lack. Both methods have their own advantages and can be used as appropriate.

@ConfigurationProperties

When the above two methods obtain the value of the object, we need to obtain it one by one, but through the * * @ ConfigurationProperties * * method, we only need to create a person entity class in the bean and assign the key of the specified configuration file in front of it.

@Data
@AllArgsConstructor
@NoArgsConstructor
//The above is the lombok plug-in
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
}

Our configuration specifies a configuration prefixed with person, i.e

name: abc
#Two ways to write objects.
person:
  name: ${name}
  age: 18

The value in this configuration.

Error encountered.

When adding @ ConfigurationProperties(prefix = "person") to the entity class, the above red prompt will appear. In fact, this is not an error, but there are also solutions. We click Open Documentation... In springboot2 There will be 404 error in 4.4. We only need to reduce the version to 2.1.8 Release to view the document.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
```![Insert picture description here](https://img-blog.csdnimg.cn/20210409130416633.png)


Paste the dependency into our pom.xml This problem can be solved.

Keywords: Java Spring Spring Boot

Added by exally on Wed, 09 Mar 2022 14:56:08 +0200