Spring Boot learning record 03_ some attribute configuration files

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 environment
  • application-test.properties:test environment
  • application-prod.properties:Production Environment

As for which specific configuration file will be loaded, it needs to be set application.propertiesthrough spring.profiles.activeproperties in the file, and its value corresponds to the {profile}value.

For example: the content of the configuration file spring.profiles.active=testwill 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.propertiesspring.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.propertiesConfigure common content in and set spring.profiles.active=devthe development environment as the default configuration
  • application-{profile}.propertiesConfigure 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

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325206016&siteId=291194637