八、profile多环境配置

通常我们的程序有着多个环境:

1、开发环境;

2、生产环境。

环境的配置各不相同,我们希望通过一个简单的配置来切换环境,而springboot轻松地实现了该功能:

一、多环境需要多配置文件

一般我们默认有着:application.properties配置文件,现在我们添加两个环境的文件,文件名格式如:application-{profile}.properties

1)application-dev.properties;// 用于开发环境

2)application-prod.properties。// 用于生产环境

下面配置数据源为例:

application-dev.properties内容如下:

# 开发环境
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://本地IP:3306/test
spring.datasource.username=root
spring.datasource.password=密码

application-prod.properties内容如下:

# 生产环境
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://服务器IP:3306/test
spring.datasource.username=root
spring.datasource.password=密码

配置好文件,我们需要激活其中一个配置文件,在application.properties添加如下配置:

# 环境配置
spring.profiles.active=prod

如上,我们激活了prod生产环境的配置文件,那么当程序启动的时候,自然就会去加载application-prod.properties文件的内容

猜你喜欢

转载自www.cnblogs.com/lay2017/p/8919868.html