Foreword
An application from development to on-line, often need to go through several stages, such as development, testing, on-line. The configuration used in each stage of the environment may have been different, Springboot applications can be easily configured to switch each environment. So, today introduces Springboot profiles multi-environment configuration switches.
Four ways to switch profiles multi-environment configuration
- Profiles of more than
- yml Multiple Document block the way
- Setup parameters
- Set up a virtual machine parameters
1, multiple configuration files
1.1, modify application.properties
server.port=8080
1.2, the master boot operation class
1.3, create application-dev.properties and application-prod.properties
application-dev.properties
server.port=8090
application-prod.properties
server.port=9090
1.4, the activation profiles in application.properties
server.port=8080
spring.profiles.active=prod
1.5, the master boot operation class
PS: If you activate dev, will use port 8090.
2, yml block mode multi-document
2.1, create application.yml
server:
port: 8080
spring:
profiles:
active: dev
---
server:
port: 8081
spring:
profiles: dev
---
spring:
profiles: prod
server:
port: 8082
---
PS: yml to "---" to separate documents block
2.2, run the main startup class
3, program arguments
3.1, setup parameters
--spring-profiles.active=prod
PS:这条指令也可以用于程序被打包成jar包后,在命令行指定激活环境。并且优先级比配置文件中高。
3.2, run the main startup class
4, set the virtual machine parameters
4.1, set the virtual machine parameters
-Dspring-profiles.active=dev
4.2, run the main startup class
to sum up
By this in several ways, Springboot program can be in different environments using different configurations of.