A preface

This is springboot configuration file introduction, is also the basic entry, today, springboot series will be complete;

Public account: Knowledge seeker

Inheriting the spirit of open Source, Spreading technology knowledge;

Springboot configuration file details

2.1 Overview of configuration Files

Springboot configuration files are divided into two formats: Properties and YML. The global configuration files are stored in the Resources directory. The essence and effect of these two configuration files are consistent, but knowledge seekers prefer YML configuration because of its simple syntax, simple and readable. Its configuration file is named application.yml or application.properties;

2.2 Syntax of the Configuration File

Properties and YML syntax are key-value pairs in nature, so it’s easy to understand. The syntax for different data types is described below.

The basic syntax is just key-value; For example, numeric types, string types, and Boolean data types are written as follows

Properties syntax example, using = to form a relationship between key and value

name = zszxz
Copy the code

Yml syntax example, use between key and value: To form a relationship

name: zszxz
Copy the code

Using @Value annotations with placeholders in Java classes conforms to the ${} read configuration file property

The sample

@Value("${name}")
private String name;
Copy the code

Note that double quotes are not recommended because they need to be escaped;

The myL syntax will be used in the following examples;

Note that there are two Spaces between the child attribute and the parent attribute.

person:
  name: zszxz
  age:18
Copy the code

List example

support:[png,text,pdf]
Copy the code

2.3 Read configuration file properties using @ConfigurationProperties

In file upload and download scenarios, you can use this approach to make your code more readable, although the knowledge Seeker gives a simple example

Defined in the configuration file

# ZSZXZ: # savePath savePath: C:/mydata/generator # downloadpath: C:/mydata/generatorCopy the code

Use @Configuration to declare FileConfig as the Configuration class and use the @ConfigurationProperties annotation to read properties prefixed with ZSZXZ from the Configuration file.

@Configuration
@ConfigurationProperties(prefix = "zszxz")
public class FileConfig {
    // File save path
    private String savePath;
    // File download path
    private String downloadpath;
    // omit set and get
}
Copy the code

This configuration allows property calls in other Java classes using Spring’s IOC-injected object approach;

Such as

@Autowired
FileConfig fileConfig;

private void init(a){
	String path = fileConfig.getSavePath();
}
Copy the code

2.4 Priority of configuration files

If multiple profiles exist in a project, the profile with a higher priority overwrites the profile with a lower priority

  1. Project root config directory:file:./config/
  2. Project root directory:file:./
  3. Config directory for classpath:classpath:/config/
  4. Class path:classpath:/(Recommended)

2.5 Configuration File External Deployment

Create a config folder in the jar directory and place the configuration file. Or simply place the configuration file in the same directory as the JAR package

Of course, you can also specify the location of the configuration file

 java -jar myproject.jar --spring.config.name=myproject
Copy the code

or

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Copy the code

Refer to the link

Docs. Spring. IO/spring – the boot…