In the development process, sometimes a set of environment is not enough, and it is very inconvenient to switch data sources and other configurations back and forth. This chapter mainly explains the configuration of multiple environment files, which has a certain reference learning value for everyone's study or work, friends in need Let's learn together with the editor
table of Contents
1. Configure multiple environments with multiple files
Profile is Spring's support for different configuration functions for different environments. It can quickly switch environments by activating, specifying parameters, etc.
In springboot, application is our main configuration file. As for the file ending in yml or the file ending in properties, both are ok, there is no substantial difference, and both are to change the springboot global default value. The only difference is the difference in grammar.
In actual development, we generally divide into the following environments. The most important thing in the so-called environment is the library. They usually have their own special test library for testing, and there will be a special online library online. These should not be confused of.
Environmental distinction:
dev: development environment
test: test environment
prod: production environment (online)
Detailed usage:
Here take the properties file as an example: create three configuration files, of which application.properties is used to specify the environment
application-environment name. This is the naming rule for multiple configuration files. ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? This suffix environment name will be used when specifying it.
application.properties
Here we specify the development environment. In actual development, the general development environment and the same configuration of the production environment will be stored in the configuration file, so that the configuration file can be managed reasonably, and it will not be so messy.
pring.profiles.active=dev
application-dev.properties
server.port=8082
application-prod.properties
server.port=80
Run test
8082 port
2. Configure multiple environments with a single file
Yml supports the differentiation of environments in the form of multiple documents. Of course, this section also requires profiles to complete the configuration of multiple files, but it does not need to create multiple files to distinguish, directly use three horizontal bars as a configuration file environment.
The following cases are divided into two environments, and then the top active is used to specify the corresponding profiles environment
spring:
profiles:
active: prod
---
server:
port: 8083
spring:
profiles: dev
---
server:
port: 8084
spring:
profiles: prod
Start the project and find that the port is 8084.
If our springboot project hits the jar package
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
We can also specify the environment in the form of this command line.