Load priority order SpringBoot profile

application.properties load

application.properties application.yml file or scan the following Spring Boot startup location as Spring Boot default configuration file

  1. file:/config/
  2. file:/
  3. classpath:/config/
  4. classpath:/

The above order of descending priority order, all the positions of the files are loaded, high priority configuration may overlay the content of the low priority configuration, wherein the configuration content file is complementary to the configuration , i.e.,

  • Configuring the presence of the same content, higher priority may overlay the content of a low priority
  • When the presence of different content, high priority and low priority content configuration taken and set

We can also be changed by spring.config.location default configuration, the specific approach is to project packed later, we can use the form of the command line parameters, when the project started to specify the new location of the configuration file, specify the configuration file and the default load the configuration file is called complementary configuration work together

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=D:/application.properties

Arranged outside the loading sequence

Spring Boot configuration may also be loaded from the following locations: descending order of priority, a high priority low priority cover, if different content, high priority and low priority complementary configuration

  1. Command line parameters

Priority is the highest command line parameters, start the port number is assumed that the internal configuration of the highest priority profile configuration is 8081, start a command line parameters such as the following settings:

java -jar spring-boot-02-config-02.0.0.1-SNAPSHOT.jar --server.port=8089

So start into a port on the 8089, the command line can put all of the configuration options of the project to get rid of all

  1. JNDI properties comp / env: From java
  2. java system properties (System.getProperties ())
  3. Operating system environment variables
  4. RandomValuePropertySource configuration random.*attribute value
  5. jar package external application-{profile}.propertiesor application.yml(带spring.profile)profile
  6. jar inside the package application-{profile}.propertiesor application.yml(带spring.profile)configuration file
  7. jar package external application.propertiesor application.yml(不带spring.profile)profile
  8. jar inside the package application.propertiesor application.yml(不带spring.profile)configuration file
  9. @PropertySource class notes on @Configuration
  10. By SpringApplication.setDefaultProperties specified default property

Read the above configuration order, let's think about a problem, if too many things I want to configure, and the project has been packaged, I command-line parameters to reconfigure too much trouble, how should I do?

看看上面的11个选项,我们会发现,2,3,4,5配置方法反而比命令行还要麻烦,所以我们可以利用6,7,8,9在项目打包之后,仍然修改配置参数

具体做法如下:

1. 首先找到项目打包好的jar包,放置到一个文件夹里面,例如app
2. 将打包好的jar包移动到这个文件夹里面
3. 在同级目录下创建一个新的application.properties文件,创建好的文件夹的目录结构如下所示:

-------------------app-------------------------
application.properties //需要重新配置的参数在这
spring-boot-02-config-02.0.0.1.SNAPSHOT.jar
-----------------------------------------------

4. 在新创建的application.properties文件里面配置我要重新配置的参数

Guess you like

Origin www.cnblogs.com/dotdashdotdash/p/12345329.html