springboot2.x basic tutorial: detailed configuration file

When using Spring Initializr to build a springboot project, the application.properties file will be automatically generated under src/main/resources. Today we will talk about SpringBoot configuration files.

The role of configuration files

SpringBoot adopts the concept of "habit is better than configuration". There are a lot of configurations in the project, and the default configuration is used, so you don't need to configure it manually.
SpringBoot can recognize configuration files in properties format and yml format (we generally use yml format more). When you need to modify the default configuration or customize the configuration , you can modify the configuration file to achieve the goal.

Basic use of configuration files

This is a configuration example using the yml format to illustrate how to configure in application.yml and how to obtain it in the code.

Number, string, Boolean get

Configuration file writing:

version: 1.0
author: codhome.vip
flag: true

Use @Value to get:

@Value("${version}")
float version;
@Value("${author}")
String author;
//这里的true为默认值
@Value("${flag:true}")
boolean flag;

Object, Map writing and access

Configuration file writing:

user:
  userName: codehome
  age: 18
  forbidden: true

Use @ConfigurationProperties to get:

@Configuration
@ConfigurationProperties(prefix = "user")
@Data
public class UserProperties {
    String userName;
    int age;
    boolean forbidden;
}
//注入使用
@Autowired
UserProperties userProperties;

List, Set, Array acquisition

Configuration file writing:

random: 10,20,30

Use @Value to get:

@Value("#{'${random}'.split(',')}")
int[] randoms; //List<Integer>也可以

Configuration file writing:

random1:
  users:
    - zhangsao
    - lisi
    - wangwu

Use @ConfigurationProperties to get:

@Configuration
@ConfigurationProperties(prefix = "random1")
@Data
public class UserProperties {
    List<String> users;
}

Summarize the difference between the two annotations

Multi-environment configuration

When we write the main configuration file, the file name can be application-{profile}.properties/yml, use spring.profiles.active to activate which configuration file to use

spring:
  profiles:
    active: dev
---

#配置开发环境
spring:
  profiles: dev
server:
  port: 9000

---

#配置生产环境
spring:
  profiles: prod
server:
  port: 9100

---

#配置测试环境
spring:
  profiles: dev
server:
  port: 8000

Profile priority

Project internal configuration file

  1. When application.properties and application.yml coexist in the same directory, the priority of properties configuration is higher
  2. The default configuration file loading order in ConfigFileApplicationListener
    Project root directory config folder>root directory configuration file>resources cofnig folder>resources configuration file
  3. When the attributes of multiple configuration files do not conflict, the configuration is complementary
  4. You can also specify the configuration file address
java -jar run-0.0.1-SNAPSHOT.jar --spring.config.location=D:/application.properties

External placement

  1. Operating system environment variables (such as the username of the operating system)
  2. Random.* property value configured by RandomValuePropertySource
  3. The application-{profile}.properties configuration file outside the jar package
  4. The application-{profile}.properties configuration file inside the jar package
  5. The application.properties configuration file outside the jar package (this level is often used in the test environment. For example, placing a configuration file in the same level directory of the jar package will cover all the configuration files inside the jar package)
  6. The application.properties configuration file in the jar package is searched from outside the jar package to the jar package, and the profile to be loaded is first loaded, and then the profile without the profile is loaded.
  7. @PropertySource on the @Configuration annotation class (manually specify to import external configuration files)
  8. The default properties specified by SpringApplication.setDefaultProperties, set in your own program code, have the lowest priority

A thousand miles begins with a single step. Here is the fifth article in the SpringBoot tutorial series. All project source codes can be downloaded on my GitHub .

Guess you like

Origin blog.csdn.net/github_35592621/article/details/108248838