Basic configuration of SpringBoot - format of yaml file and data reading

SpringBoot basic configuration

Configuration file format

Let's use the example of modifying the server port number to demonstrate the format of the configuration

At present, our SpringBoot starter program can be started, but the port is the default 8080

http://localhost:8080/books/1

Modify the port number of the server to 80

http://localhost/books/1

SpringBoot provides a variety of property configuration methods :

Method 1: Configure in the application.properties file under the resource folder

server.port=80

Method 2: Create an application.yml file configuration under the resource folder

server:
  port: 80

Method 3: Create an application.yaml file configuration under the resource folder

server:
  port: 80

Among the three methods, we most often write the second one: create an application.yml file for configuration

SpringBoot configuration file loading order and priority (understand)

application.properties > application.yml > application.yaml

Note :

The SpringBoot core configuration file is named application

There are too many built-in properties in SpringBoot, and all the properties are modified together. When using it, modify the properties through the prompt key + keyword

yaml file format

YAML (YAML Ain't Markup Language), a data serialization format, let's compare the configuration files of XML, Properties and YAML :

XML

<enterprise>
  <name>chenyq</name>
  <age>16</age>
  <tel>4006184000</tel>
</enterprise>

Properties

enterprise.name=chenyq
enterprise.age=16
enterprise.tel=4006184000

YAML

enterprise:
	name: chenyq
	age: 16
	tel: 4006184000

Advantages :

easy to read

Easy to interact with scripting languages

Take data as the core, pay more attention to data than format

yaml file extension

.yml (mainstream)

.yaml

yaml file syntax rules

Case Sensitive

The attribute hierarchy is described in multiple lines, and the end of each line is terminated with a colon

Use indentation to indicate hierarchical relationships, align to the left of the same level, and only spaces are allowed (Tab keys are not allowed)

Add a space before the attribute value (a colon + space is used as a separator between the attribute name and the attribute value)

# means comment

Important note: the data must be separated by a space and a colon

yaml array data :

The array data uses a minus sign as the data start symbol below the data writing position, and writes one data per line, and the minus sign and the data are separated by spaces

enterprise:
  name: chenyq
  age: 16
  tel: 12312345678
  subject:
    - Java
    - 前端
    - 大数据

yaml data read

For example, the data in the application.yml file is as follows :

enterprise:
  name: chenyq
  age: 16
  tel: 12312345678
  subject:
    - Java
    - 前端
    - 大数据

Method 1: Use @Value to read a single data, attribute name reference method:${一级属性名.二级属性名……}

@RestController
@RequestMapping("/books")
public class BookController {
    
    
  	// 读取文件中的数据
    @Value("${enterprise.name}")
    private String name;

    @GetMapping("/{id}")
    public String selectById(@PathVariable Integer id) {
    
    
        System.out.println(name);
        return "hello spring boot";
    }
}

Read the array data in the file

@RestController
@RequestMapping("/books")
public class BookController {
    
    
    @Value("${enterprise.subject[0]}")
    private String subject1;
    @Value("${enterprise.subject[1]}")
    private String subject2;

    @GetMapping("/{id}")
    public String selectById(@PathVariable Integer id) {
    
    
        System.out.println(subject1); // Java
        System.out.println(subject2); // 前端
        return "hello spring boot";
    }
}

Method 2: The method of reading data in method 1 is too scattered, we can encapsulate all the data in the file into the Environment object ( commonly used in the framework )

Then read the properties through the getProperty method of the Environment object

@RestController
@RequestMapping("/books")
public class BookController {
    
    
    @Autowired
    // 读取全部数据封装到environment对象中
    private Environment environment;

    @GetMapping("/{id}")
    public String selectById(@PathVariable Integer id) {
    
    
      	// 再通过Environment对象的getProperty方法读取属性
        System.out.println(environment.getProperty("enterprise.name"));
        System.out.println(environment.getProperty("enterprise.subject[1]"));
        return "hello spring boot";
    }
}

Method 3: Customize the object, encapsulate the entity class, the attributes of the entity class correspond to the data to be read one by one, and read after encapsulation (commonly used )

Define the entity class as a bean, and configure the current object to read the enterprise attribute from the configuration;

@Component // dingyiweibean
@ConfigurationProperties(prefix = "enterprise") // 读取配置中的enterprise属性
public class Enterprise {
    
    
    private String name;
    private Integer age;
    private String tel;
    private String[] subject;
}

At this point, the properties of the entity class and the configuration file will correspond one-to-one, and we only need to use the Enterprise object to use the data

@RestController
@RequestMapping("/books")
public class BookController {
    
    
    @Autowired
  	// 自动装配Environment对象
    private Environment environment;

    @GetMapping("/{id}")
    public String selectById(@PathVariable Integer id) {
    
    
      	// 通过对象可以获取数据
        System.out.println(enterprise);
        return "hello spring boot";
    }
}

Custom Object Encapsulation Data Warning Solution

We have the following warning when we customize the class

insert image description here

Solution: Add the following dependencies to the pom.xml file to solve the problem

<!--解决警告-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/128053379