The use of springBoot configuration files

  1. Overview

  • file type

There are three configuration files

– application.properties

– application.yml (recommended)

– application.yaml

  • file priority

When all three configuration files exist, the configuration priority of a certain function

application.properties > application.yml > application.yaml

  • How to write the document

  • application.properties

serve.port=8080
  • application.yml : English colons must be followed by a space followed by configuration data (the port is followed by a space followed by port 80 data), and if there is a hierarchy, it is separated by a space (not a tab, for example, the following port is preceded by a space instead of a tab)

server:
  port: 80 #There must be a space after the colon, and a space before it

subject:
    - Java
    - front end
    - Big Data

enterprise:
    name: itcast
    age: 16
    subject:
        - Java
        - front end
        - Big Data
likes: [Glory of the King, stimulating the battlefield] #Array writing abbreviated format

users: #object array format one
  -name: Tom
       age: 4
  -name: Jerry
    age: 5

users: #object array format 2
  -
    name: Tom
    age: 4
  -
    name: Jerry
    age: 5
          
users2: [ { name:Tom , age:4 } , { name:Jerry , age:5 } ] #Short form of object array
  • application.yaml: There is basically no difference in use from .yml

server:
  port: 80 #There must be a space after the colon, and a space before it
  1. Custom configuration file

(1) Configuration in direct project management

Under normal circumstances, the above three files created directly under the resources folder will be automatically recognized as configuration files (the file name cannot be wrong), we can also name it ourselves and set it as a configuration file

  • a. Create a yml file named by yourself (the other two are also available), here is myPro, yml

  • b. Enter the project structure (Project Structure)

  • c. Click the spring logo, circled

  • d. Click Add Custom Profile

  • Then select your own configuration file, click OK, apply, and the spring logo will come out.

(2) Command line configuration

Direct write via direct parameter

  1. Get configuration file information

Note: The yml configuration file is used here, and other self-test, the following is the custom configuration

lesson: Spring Boot

# object type
user:
  id: 1
  name: admin
  password: 123456

# list type
subject:
  -JAVA
  - C++
  -JS
# subject: [JAVA,C ++ ,JS] Same effect as above

Read single data

The form of reading data ${first-level attribute name.second-level attribute name…}

@Value("${lesson}")
private String lesson;

@Value("${user.id}")
private String userId;

@Value("${subject[1]}")
private String subject_01;

// Spring Boot
// 1
// C++ 

Read all data

The data is encapsulated into the Environment object. When obtaining the property, it is performed through the interface operation of the Environment. The specific method is getProperties (String), and the parameter can be filled with the property name

// read all configuration information
@Autowired
private Environment env;

System.out.println(env.getProperty("lesson"));
System.out.println(env.getProperty("user.id"));
System.out.println(env.getProperty("subject[0]"));

// result
Spring Boot
1
JAVA

Read data object

  • Create a configuration object class, here create a user configuration class

user:
  id: 1
  name: admin
  password: 123456
@Component
// prefix is the prefix user, corresponding to the above form => first-level attribute name. second-level attribute name
@ConfigurationProperties(prefix = "user")
public class UserProperties {
    private int id;
    private String name;
    private String password;
}

May need to introduce coordinates (ConfigurationProperties)

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
  • Use configuration objects

@Autowired
private UserProperties userProperties;

System.out.println(userProperties.getId());
System.out.println(userProperties.getName());
System.out.println(userProperties.getPassword());

Data mutual reference in yml file

center:
    dataDir: D:/usr/local/fire/data
    tmpDir: D:/usr/local/fire/tmp
    logDir: D:/usr/local/fire/log
    msgDir: D:/usr/local/fire/msgDir

Directly through ${first-level attribute name. Second-level attribute name…}

baseDir: /usr/local/fire
center:
 dataDir: ${baseDir}/data
 tmpDir: ${baseDir}/tmp
 logDir: ${baseDir}/log
 msgDir: ${baseDir}/msgDir
  1. Set temporary attributes

Note: After the configuration file is written, there may be temporary configuration information changes. At this time, we need to set temporary configuration information

  • Add corresponding parameters when starting, multiple attributes are separated by spaces

java –jar springboot.jar –-server.port=80 --logging.level.root=debug
  1. Multi-environment development issues

(1) Single file

This is relatively rare

spring:
    profiles:
        active: pro # start pro
---
spring:
    profiles:pro
server:
    port: 80
---
spring:
    profiles: dev
server:
    port: 81

(2) Multi-file development yml

Naming rules application-environment name.yml, properties version is written differently from this one, so I won’t write it here

  • The main configuration file application.yml

spring:
    profiles:
        active: pro # start pro
  • Development environment configuration file application-dev.yaml

server:
    port: 80
  • Production environment configuration file application-pro.yaml

server:
    port: 81
  • Test environment configuration file application-test.yaml

server:
    port: 82

(3) Independent configuration file

In order to facilitate further management, you can refine

  • The main configuration file application.yml

spring:
    profiles:
        active: dev
        group:
            "dev": devDB, devRedis, devMVC
            "pro": proDB, proRedis, proMVC
            "test": testDB, testRedis, testMVC
  • Create another configuration file name below, under the same folder

application-dev.yaml

application-devDB.yaml

application-devRedis.yaml

application-devMVC.yaml

application-pro.yaml

application-proDB.yaml

application-proRedis.yaml

application-proMVC.yaml

application-test.yaml

application-testDB.yaml

application-testRedis.yaml

application-testMVC.yaml

6. Classification of files

  • profile level

– Configuration file under the class path (this is what I have been using, that is, the application.yml file in the resources directory)

– Configuration files in the config directory under the classpath

– Configuration files in the directory where the package is located

– The configuration file under the config directory in the directory where the package is located

  • priority

1. file: config/application.yml
【Highest】

2. file: application.yml

3. classpath: config/application.yml

4. classpath: application.yml
【lowest】

  • Location

classpath: the location distribution of configuration files in the directory where the package is located

file: Under the class path, after packaging, as long as it is in this form of file structure with the jar package, the configuration file level in config is higher than that outside, but it has a higher priority than the configuration file in the project