Turn from: http://blog.didispace.com/springbootproperties/
Multi-environment configuration (this place is a bit like Maven's profile configuration)
When we develop spring boot applications, the same set of programs is usually applied and installed into several different environments, such as: development, testing, production, etc. Each environment's database address, server port and so on configuration will be different, if you are packaging for different environments to frequently modify the configuration file, it will be a very cumbersome and prone to error.
For a multi-environment configuration, the basic idea of a variety of project building tools or frameworks is consistent, and the Spring boot is no exception, or simpler, by configuring multiple profiles of different environments and then packaging them to specify what needs to be packaged.
In spring boot {profile}
, the Multi-environment profile file application-{profile}.properties
name needs to be in a format that corresponds to your environment identity, such as:
application-dev.properties
: Development environment
application-test.properties
: Test environment
application-prod.properties
: Production environment
As to which specific configuration file will be loaded, it needs application.properties
to be set spring.profiles.active
in the file by property, and {profile}
its value corresponds to the value.
such as spring.profiles.active=test
: The configuration application-test.properties
file contents will be loaded
Below, a sample experiment is performed with different service ports configured in different environments.
- Create a different profile
application-dev.properties
application-test.properties
for each environment,application-prod.properties
- All three files are set with
server.port
different properties, such as: Dev environment set to 1111,test environment set to 2222,prod environment set to 3333
- Application.properties, which means that the dev environment is set by default
spring.profiles.active=dev
According to the above experiment, we can summarize the multi-environment configuration ideas as follows:
application.properties
The common content is configured in and spring.profiles.active=dev
set to the development environment as the default configuration
application-{profile}.properties
Configure different contexts in each environment
- By default, the lower port is 8080.
- Set server.port=8090 in application.properties
- Add application-dev.properties, and set server.port=8070; application.properties settings Spring.profiles.active=dev
Spring Boot Learning record 03_ some property profiles