2
It is better to teach people how to fish than to teach people how to fish. Hello everyone, I am Java class representative . Today I will share with you some of the configuration of SpringBoot based on profile.
For more information on Java, please follow the public : 160f7a2d1949da Java class representative .

0 pain points

In the development process, we generally have several different environments, such as development environment and production environment. In general, the program configuration in different environments is different. If you modify the configuration file every time you switch the environment, it is troublesome and error-prone.

For example: in the development stage, we can use the embedded H2 database, but use mysql in the production environment.

1 YAML file

SpringBoot uses YAML (/ˈjæməl/) files to manage the configuration, which has the advantages of conciseness and good readability.

There are two ways to implement multiple configurations:

  1. Write different configurations in a single yml configuration file
  2. Write multiple yml files, representing different configurations

Single yml file

In one yml file by --- plurality of spaced different configurations, according to spring.profiles.active values to determine which configuration is enabled, for example:

#公共配置
spring:
  profiles:
    active: pro #使用名为pro的配置,这里可以切换成dev
  datasource:
    url: jdbc:mysql://localhost:3306/test_db?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
    password: 123456
    username: root
---
#开发环境配置
spring:
  profiles: dev #profiles属性代表配置的名称

server:
  port: 8080
---
#生产环境配置
spring:
  profiles: pro

server:
  port: 80

Multiple yml files

Write the configuration in multiple yml files, and decide which configuration to enable spring.profiles.active

The configuration file rules are as follows:

  1. File naming needs to conform to the format of application-{profile}.yml, such as: application-dev.yml
  2. The same configuration is written to application.yml, and different environment configurations are written to different yml files

E.g:

application.yml

#公共配置
spring:
  profiles:
    active: pro #使用名为pro的配置,这里可以切换成dev
  datasource:
    url: jdbc:mysql://localhost:3306/test_db?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
    password: 123456
    username: root

application-dev.yml

server:
  port: 8080

application-pro.yml

server:
  port: 80

2 Choose a configuration

With the configuration of the YAML file, the different configurations of different environments finally do not need to be changed, but you should still pay attention to specifying spring.profiles.active when submitting the code, otherwise there is a risk of incorrect configuration. Is there a solution once and for all?

When we start the application, we specify which configuration to use through environment variables, as follows:

java -jar xxx.jar --spring.profiles.active=pro

Through this configuration, we can specify that the dev configuration is used in the development environment, and the pro configuration is used in the production environment, so as to solve the problem of incorrect configuration files at one time, and it is also very elegant!

3 matters needing attention

  • The two configuration methods are used according to personal preference, but if there are many configurations, it is recommended to split into multiple yml files, that is, method two. Easy to maintain
  • Pay attention to distinguish the difference between the two, can not mix

4 references

  • Spring combat (fifth edition)
  • Spring official document

[Recommended reading]
RabbitMQ tutorial
Freemarker Tutorial (1)
downloaded attachment name is always garbled? You should read the RFC document!
MySQL priority queue in a simple way (the order by limit problem you will definitely step on)


The code word is not easy, welcome to like, follow and share.
Search: [Java class representative], follow the official account, update every day, and get more Java dry goods in time.


Java课代表
640 声望1k 粉丝

JavaWeb一线开发,5年编程经验