[Spring Boot] (8), external configuration loading order

7. Configuration file loading location

The previous section described Spring Boot loading configuration files from within the project, and this section focuses on loading configuration files from outside.

Spring Boot can also load configurations from the following locations; priority from high to low; high-priority configurations override low-priority configurations, and all configurations will form complementary configurations.

  1. command line arguments

All configurations can be specified on the command line

java -jar spring-boot-02-config-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc

Multiple configurations are separated by spaces; --configuration item=value

Of course, since it is not necessarily safe to use command-line parameters or modify the default property configuration, you can prohibit the use of the command-line through code.

SpringApplication application = new SpringApplication(SpringBoot02ConfigApplication.class);

//Prohibit modifying default configuration properties through command line parameters
application.setAddCommandLineProperties(false);

//start up
application.run(args);
  1. JNDI properties from java:comp/env

  2. Java System Properties (System.getProperties())

  3. operating system environment variables

  4. random.* property values ​​configured by RandomValuePropertySource

Search from outside the jar package ---- to the ----> jar package;

Priority loading with profile

  1. The application-{profile}.properties or application.yml (with spring.profile) configuration file outside the jar package

  2. The application-{profile}.properties or application.yml (with spring.profiles) configuration file inside the jar package

Then load without profile

  1. The application.properties or application.yml (without spring.profile) configuration file outside the jar package

  2. The application.properties or application.yml (without spring.profile) configuration file inside the jar package

  1. @PropertySource on @Configuration annotated classes

  2. Default properties specified via SpringApplication.setDefaultProperties

// use the specified configuration file
SpringApplication application = new SpringApplication(SpringBoot02ConfigApplication.class);

//Load the specified configuration file
InputStream is = SpringBoot02ConfigApplication.class.getClassLoader().getResourceAsStream("app.properties");
Properties properties = new Properties();
try {
    properties.load(is);
} catch (IOException e) {
    e.printStackTrace ();
}

// set properties
application.setDefaultProperties(properties);
//start up
application.run(args);

All supported configuration loading sources: refer to the official documentation

=====================Make an advertisement, welcome to pay attention =====================

QQ:
412425870
WeChat public account: Cay Classroom

csdn blog:
http://blog.csdn.net/caychen
Code cloud:
https://gitee.com/caychen/
github:
https://github.com/caychen

Click on the group number or scan the QR code to join the QQ group:

328243383 (1 group)




Click on the group number or scan the QR code to join the QQ group:

180479701 (2 groups)





            

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325708823&siteId=291194637