[Java development framework SpringBoot] Configuration Profile multi-environment support (differentiation of development environment and production environment configuration)

SpringBoot configuration profile multi-environment support

In our daily development, there are basically two environments: the development and test environment developed by ourselves and the production environment deployed online. But when there is only one configuration file, it is very troublesome to modify the configuration file back and forth. And SpringBoot helped us solve this trouble.

The configuration is very simple. First, create two configuration files and distinguish them by dev(development environment) and pro(production environment).

Insert picture description here
可以看到一共有3个文件

application.yml: Configure some configurations that are the same as the development environment and the production environment. For example, if the port number is both 8080, you can write it here.
application-dev.yml: The configuration used in the development environment. For example, local database configuration, local redis configuration, etc.
application-pro.yml: The configuration used in the production environment. For example, online database configuration, online redis configuration, etc.

想要让dev或pro文件生效,必须在application.yml中配置一个属性

# application.yml
spring:
  profiles:
    active: dev

Start SpringBoot

Insert picture description here
可以看到启动之后如果配置的是dev则会使用dev文件的配置,如果配置的是pro则会使用pro文件的配置。这样就不会有来回修改环境带来的不便。

to sum up

如果觉得不错,可以点赞+收藏或者关注下博主。感谢阅读!

Guess you like

Origin blog.csdn.net/weixin_42825651/article/details/109217073