I. Benefits of multi-environment configuration
1. Different environment configuration can be configured with different parameters
2. Easy to deploy, improve efficiency, reduce errors
Second, the properties of multi-environment configuration
1. Configure activation options
Spring.profiles.active=dev
Description: The development environment is activated here.
2. Add additional Profiles
Such as:
Development environment: Application-dev.properties
Test environment: Application-test.properties
Production environment: Application-prod.properties
Public Environment: Application.properties
Warm tips:
1, generally we put the public configuration in the public environment, each environment is unique in the corresponding environment, configuration activation options are generally configured in the public environment.
2. If the configuration of the public environment is in conflict with the configuration of the activation options environment, the other environment will overwrite the public environment configuration.
Three, YAML Multi-environment configuration
1. Configure activation options
Spring:profiles:active:dev
2. In the configuration file add three in the English state of the dash to distinguish between different configuration environment
---
Spring
Profiles:dev
Cases:
Server: port: 80 tomcat: uri-enconding: utf-8spring: profiles: active: dev jackson: date-format: yyyy-mm-dd hh:mm:ss time-zone: asia/chongqingroncoo: secret: ${random.value} number: ${random.int} number2: less: than: ten: ${random.int (} number3: in: range): ${random.int[1024,65536]} name: zhangsan desc: ${roncoo.name} is a domain name---spring: profiles: active: testserver: port: 9090---spring: profiles: active: devserver: port: 8989
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/98/A7/wKioL1k_U3KzQ8_RAAAhwB30S1k053.jpg "title=" 1497322295 (1). jpg "alt=" wkiol1k_u3kzq8_raaahwb30s1k053.jpg "/>
Comparison of two configuration modes
1, the Properties configuration multi-environment, need to add multiple configuration files, Yaml only need one accessory file
2, the difference in writing format, Yaml relatively concise, elegant
3, the disadvantage of Yaml: Can not be loaded by @propertysource annotations. If you need to load values using @propertysource annotations, use the properties file.
Five, after packaging, run
Such as:
Application-dev.properties Port: 8080
Application-test.properties Port: 9090
Application.properties Port: 80
Packaged:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/98/7D/wKioL1k9QwGj6z1ZAAAoMsW3vEk153.png "title=" 1497187016 (1). png "alt=" Wkiol1k9qwgj6z1zaaaomsw3vek153.png "/>
Run:
Java-jar running the jar--spring.profiles.active=dev
Such as:
Java-jar Spring-boot-demo-2-1-0.0.1-snapshot.jar--spring.profiles.active=dev
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/98/7E/wKiom1k9Q-PAqXWKAABi5nPDsRI648.jpg "title=" 1497187243 (1). jpg "alt=" wkiom1k9q-paqxwkaabi5npdsri648.jpg "/>
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/98/7E/wKiom1k9RCvhMqFZAAAeOpTJ1po854.jpg "title=" 1497187320.jpg "alt=" Wkiom1k9rcvhmqfzaaaeoptj1po854.jpg "/>
Note: If the jar is run directly "Java-jar", then the configuration inside the application.properties is used, if the configuration inside the application.properties has a specified environment, then the specified environment is run.
This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1934810
Springboot (3): Configuration file-Multi-environment configuration