SpringBoot configuration file

1. The role of the configuration file

All important data in the entire project are configured in configuration files, such as:

  • Database connection information (including user name and password settings);
  • The startup port of the project;
  • Information such as the calling key of the third-party system;
  • Common logs and exception logs for finding and locating problems.

Imagine that if there is no configuration information, then the Spring Boot project cannot connect and operate the database, or even save key logs that can be used for troubleshooting, so the role of configuration files is very important.

2. The format of the configuration file

2.1 properties

The properties configuration file is the earliest configuration file format, and it is also the default configuration file for creating a Spring Boot project

2.1.1 Basic syntax

Properties are configured in the form of key values, and the key and value are connected by "=", such as:

# 配置项⽬端⼝号
server.port=8080
# 数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456

2.1.2 Read configuration file

If you want to actively read the content in the configuration file in the project, you can use the @Value annotation to achieve it.
The @Value annotation is read using the "${}" format, as shown in the following code:
configuration file:
insert image description here

code:

@RestController
public class TestController {
    
    
    @Value("${name}")
    private String name;
    @PostConstruct
    public void getName() {
    
    
        System.out.println(name);
    }
}

The execution result is:
insert image description here

2.2 yml

2.2.1 Concept

yml is YAML is an abbreviation, its full name Yet Another Markup Language translated into Chinese is "another markup language".
Analysis of the advantages of yml:

  • yml is highly readable, simple to write, and easy to understand. Its syntax is similar to the JSON language.
  • yml supports more data types, it can simply express list (array), hash table, scalar and other data forms. It uses whitespace symbols for indentation and features that rely heavily on appearance, and is especially suitable for expressing or editing data structures, various configuration files, etc.
  • yml supports more programming languages, it can be used in Golang, PHP, Python, Ruby, JavaScript, Perl not only in Java

2.2.2 Basic syntax

yml is a tree-structure configuration file, its basic syntax is "key: value", note that the key and value are composed of English colons and spaces, and the spaces cannot be omitted. yml can also be
insert image description here
configured Primitive types and null

# 字符串
string.value: Hello
# 布尔值,true或false
boolean.value: true
boolean.value1: false
# 整数
int.value: 10
int.value1: 0b1010_0111_0100_1010_1110 # ⼆进制
# 浮点数
float.value: 3.14159
float.value1: 314159e-5 # 科学计数法
# Null,~代表null
null.value: ~

yml reads the configuration in the same way as properties, just use the @Value annotation
Note: the string does not need single or double quotes by default, if you add single and double quotes in English, it can express special meaning.

2.2.3 Configuration objects

Configuration objects are also supported in yml, the syntax is as follows:

student:
 id: 1
 name: Java
 age: 18

You can also use inline syntax:

student: {
    
    id: 1,name: Java,age: 18}

At this time, you cannot use @Value to read the objects in the configuration. At this time, you need to use another annotation @ConfigurationProperties to read. The specific implementation is as follows:

@ConfigurationProperties("student")
@Component
@Data
public class Student {
    
    
    private int id;
    private String name;
    private int age;
}
@Component
public class StudentComponent {
    
    
    @Autowired
    private Student student;

    @PostConstruct
    public void getStudent() {
    
    
        System.out.println(student);
    }

}

Results of the:
insert image description here

2.2.4 Configuration collection

The configuration file can also configure the list collection, as follows:

students:
  name:
    - 张三
    - 李四
    - 王五

You can also use inline notation:

students: {
    
    name: [张三, 李四, 王五]}

Like objects, collections are read using @ConfigurationProperties

@Component
@ConfigurationProperties("students")
@Data
public class ListConfig {
    
    
 private List<String> name;
}

2.3 Comparison between properties and yml

  • properties is a key-value type configuration file configured in the form of key=value, while yml is configured in a tree configuration similar to json format, and the yml level uses a newline indentation method For configuration, use ":" English colon plus space between key and value, and the space cannot be omitted
  • properties is an early and default configuration file format, but its configuration has certain redundant data, using yml can solve the problem of data redundancy very well
  • yml is more versatile and supports more languages, such as Java, Go, Python, etc. If it is cloud server development, you can use a configuration file as a common configuration file for Java and Go
  • yml supports more data types

Set configuration files for different environments

  1. Create configuration files for different environments:
  • application-dev.yml (development environment)
  • application-prod.yml (running environment)
    note: the prefix of these two files must be application
  1. Set up the running environment in application.yml
spring.profiles.active=dev

Guess you like

Origin blog.csdn.net/m0_71645055/article/details/132323959