Reprinted from: http://blog.didispace.com/springbootproperties/
Multi-environment configuration (this place is somewhat similar to maven's profile configuration)
When we develop Spring Boot applications, usually the same set of programs will be applied and installed in several different environments, such as: development, testing, production, etc. The configuration of the database address, server port, etc. for each environment will be different. If the configuration file must be frequently modified when packaging for different environments, it will be a very tedious and error-prone thing.
For multi-environment configuration, the basic ideas of various project construction tools or frameworks are the same. By configuring multiple configuration files for different environments, and then specifying the content to be packaged through the packaging command, the packaging is differentiated, and Spring Boot is no exception. , or even simpler.
The format that the multi-environment configuration file name needs to meet in Spring Boot application-{profile}.properties
, which {profile}
corresponds to your environment identifier, such as:
application-dev.properties
: Development environmentapplication-test.properties
:test environmentapplication-prod.properties
:Production Environment
As for which specific configuration file will be loaded, it needs to be set application.properties
through spring.profiles.active
properties in the file, and its value corresponds to the {profile}
value.
For example: the content of the configuration file spring.profiles.active=test
will be loadedapplication-test.properties
Below, take the configuration of different service ports in different environments as an example to conduct sample experiments.
- Create different configuration files for each environment
application-dev.properties
,application-test.properties
,application-prod.properties
- Different properties are set in these three files
server.port
, such as: dev environment is set to 1111, test environment is set to 2222, prod environment is set to 3333 - Set in application.properties
spring.profiles.active=dev
, which means that the default is set in the dev environment
According to the above experiments, the configuration ideas of multiple environments can be summarized as follows:
application.properties
Configure common content in and setspring.profiles.active=dev
the development environment as the default configurationapplication-{profile}.properties
Configure different content for each environment in- By default the port is 8080
- Set server.port=8090 in application.properties
- Add application-dev.properties and set server.port=8070 ; set spring.profiles.active=dev in application.properties