Spring boot project notes 3-environment switching configuration

In the development stage of our project, the database link information of the local development link is different from the database link information of the official environment. When the official environment is used, the configuration file of the database needs to be modified. If you forget to modify it, it will be a big problem. The switching is caused by different configuration file names. Let's take a look at the tests we have done.

1: Add in the application.yml configuration file:

spring:
  profiles:
    active: prod

There are also profiles configuration in springMVC, but in two different parts in xml. But in springboot, it can be divided into two files to store different configuration information.

2. After this configuration, when reading the configuration file, the original dbConfig.properties is divided into two files

dbConfig-dev.properties

dbConfig-prod.properties

Two files, where the value after "-" must be exactly the same as the configuration in application.yml

3. Modify the annotation PropertySource of the model to read the configuration file, as follows:

@PropertySource("classpath:dbConfig-${spring.profiles.active}.properties")
The value of ${spring.profiles.active} is the value of the key configured in application.yml

In this way, the value of the model is determined by spring.profiles.active in the application.yml file. After packaging the jar package, specify what spring.profiles.active is when running the jar

 

Guess you like

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