Switching use of springboot multiple configuration files

Traditional usage method ( especially before springboot2.4 version)

Step 1: Customize the configuration file application-[anything].properties
            For example:
                 Create a new debugging environment configuration file: application-dev.properties
                 Create a new production environment configuration file: application-prod.properties

Step 2: Switch in application.properties
            , such as switching to the production environment: spring.profiles.active=prod

After the springboot2.4 version, the multi-environment configuration is used in a file application.yml ( it seems that application.properties cannot be used ):

Step 1. Use --- to separate the configuration of different environments, for example:

---
spring:
  config:
    activate:
      on-profile: dev
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/database-name?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai
    username: user_Zhang
    password: user_Zhang's password
    tomcat:
      max-active: 50
  # Show each sql query
  jpa:
    show-sql: true
---
spring:
  config:
    activate:
      on-profile: prod
  datasource:
    url: jdbc:mysql://生产环境的数据库服务器ip:3306/databasae-name?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai
    username: root
    password: root_secret

Step 2. Open the idea startup configuration dialog box, Active Profiles: fill in the configuration to be activated currently, such as the configuration parameters for activating the development environment, fill in dev, and click apply.

If it is started by the java -jar command, add --spring.profiles.active with a space after the jar package name, for example:

java -Xms512m -Xmx1g my.jar --spring.profiles.active=prod

I refer to the following articles:

24. Externalized Configuration

Uploading Chinese configuration in IntelliJ idea configuration application.properties to MySQL database is garbled

Properties files | IntelliJ IDEA

Spring Boot - Application Properties

https://www.baeldung.com/spring-profiles

Spring Boot Config Data Migration Guide · spring-projects/spring-boot Wiki · GitHub

Config file processing in Spring Boot 2.4

What is diff between spring.profiles.active vs spring.config.activate.on-profile? - Stack Overflow

Guess you like

Origin blog.csdn.net/miaowansheng/article/details/127133921