Getting started with spring boot and yaml configuration

Getting started with SpringBoot

Create a SpringBoot project: file new project, click Spring Initializr on the left in the figure, and then just fill in the project name and other information.

After creation, a startup class of SpringBoot project will be automatically produced:

@SpringBootApplication
public class SpringbootDemo01Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemo01Application.class, args);
    }

}

The automatic assembly process of SpringBoot is closely related to the annotation @ SpringBootApplication. If the relevant dependent packages are imported, the corresponding configuration will be completed automatically by SpringBoot. For example, if the jar packages related to SpringMVC are imported, the disp catcher servlet and view parser will be configured by SpringBoot without manual configuration by the developer.

pom. There is a parent project in the XML file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

There are also two dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

The first dependency is related to the Web project (because spring Web is checked during creation), and the jar package related to spring MVC is imported. The second dependency is related to testing. Generally, there is also a dependency (spring boot starter), but because spring Web was selected before, it may not be necessary in the pom file. The dependency is:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

Getting started with yaml configuration

After the SpringBoot project is created, it will automatically produce an empty configuration file: application Properties, but spring boot recommends using yaml for configuration. Yaml syntax is simple and can easily configure complex values such as arrays, collections and objects. Add the existing entity class Person as follows:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
    private String name;
    private int age;
    private boolean isHappy;
    private List<Integer> list;
    private Map<String, String> map;
    private Dog dog;
}

yaml is configured as follows:

server:
  port: 8031
person:
  name:
    xxx
  age:
    10
  isHappy:
    false
  list:
    - 2
    - 5
    - 7
  map:
    k1: v1
    k2: v2
  dog:
    name: Wangcai
    age: 3

Then enter the person object and the result is: Person(name=xxx, age=10, isHappy=false, list=[2, 5, 7], map={k1=v1, k2=v2}, dog=Dog(name = Wangcai, age=3)), that is, all the corresponding configurations in yaml are injected into the attributes of the person object@ The prefix property in the ConfigurationProperty means to obtain the object name in the yaml configuration file (person here), while the yaml syntax has strict requirements for spaces. A root object is at the beginning of a line without spaces in front. If there are spaces, it indicates that the line is one of the properties. The colon in the middle of the k-v key value pair of yaml must be empty, and each value of the array must be preceded by -, and there must be a space after -.

Note that the yaml file name is application Yaml, which can not be changed to other names in general. This is also the embodiment that the Convention of SpringBoot is greater than the configuration. If you have to change it, add the annotation @ PropertySource(value = "classpath: configuration file name") on the entity class. In the above yaml configuration, the port of tomcat is also modified, which is generally 8080, and here it is modified to 8081.

The form of EL expression is also supported in yaml:

person:
  name:
    xxx${random.uuid}
  age:
    ${random.int}
  isHappy:
    false
  list:
    - 2
    - 5
    - 7
  map:
    k1: v1
    k2: v2
  dog:
    name: ${person.hello:hello}_wangcai
    age: 3

In the above yaml file, the name is followed by a uuid and the age is a random integer. If the person's Hello attribute has a value, the dog's name is also the value of the Hello attribute, otherwise it is hello. The running result is: Person(name=xxx10372281-125d-492f-9a43-4af7c362d928, age=-635582644, isHappy=false, list=[2, 5, 7], map={k1=v1, k2=v2}, dog=Dog(name=hello_wangcai, age=3))

Keywords: Java Spring Spring Boot intellij-idea

Added by wacook on Sun, 20 Feb 2022 18:30:50 +0200