Different environments based on Spring Boot use different configuration methods

  • 2020-12-18 01:50:03
  • OfStack

spring Multi-file configuration:

1. properties file

2. YAML file

1. properties file

In Spring Boot, the filename of the multi-environment configuration needs to be application-{profile}.

properties format, where {profile} corresponds to your environment id, as shown below.

The & # 8226; application-dev.properties: Development environment.

The & # 8226; application-test. properties: Test environment.

The & # 8226; application-prod.properties: Production environment.

As for which configuration file will be loaded, it needs to be passed in the app Giant ca giant ES40en.properties file

The spring.profiles.active attribute is set with the value corresponding to the {profile} value in the configuration file. Such as

spring. profiles. active = test loads the ES54en-ES55en. properties configuration

File contents.

2. YAML file


server: 
 port: 8080 
#  The default profile for dev , other environments use different boot parameters by specifying them profile , such as:  
#  Test environment: java -jar xxx.jar --spring.profiles.active=test 
#  Production environment: java -jar xxx.jar --spring.profiles.active=prod 
spring: 
 profiles: 
 active: dev 
# The following 1 Rows must not be less, distinguish between different configurations, and must be 3 A character "-"
---
#  Development environment configuration 
spring:
 profiles: dev
 datasource:
 url: jdbc:mysql://192.168.0.152:3306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false

---
#  Test environment configuration 
spring:
 profiles: test
 datasource:
 url: jdbc:mysql://192.168.0.152:13306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false

---
#  Production environment configuration 
spring:
 profiles: prod
 datasource:
 url: jdbc:mysql://192.168.0.152:23306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false

Usage:

Use a different profile by specifying startup parameters, such as:

Test environment: ES70en-ES71en xxx.jar? spring. active=test

Production environment: java jar xxx.jar?? spring.profiles =prod


Related articles: