spring.profiles.active和spring.profiles.include的使用与区别(Usage and difference between spring.profiles.active and spring.profiles.include)

spring.profiles.active和spring.profiles.include的区别与使用

  • 业务场景
  • 优化方式
  • spring.profiles.active属性
  • 启动时指定:
  • spring.profiles.include属性
  • 配置方法:
  • 配置的位置和区别:
  • 用示例来使用和区分
  • 测试一:
  • 测试二:
  • 测试三:

业务场景

一套程序可能会被部署到不同的环境。开发、测试、生产环境有不同的配置信息,包括jdbc地址、ip、端口等。如果在同一个配置文件中,每次部署去改配置,会非常麻烦且容易出错。

优化方式

spring.profiles.active属性

一个好方法就是创建不同的配置文件,且命名规则遵循application-${profile}.properties,例如:

  • 开发环境配置文件:application-dev.properties
  • 测试环境配置文件:application-test.properties
  • 生产环境配置文件:application-prod.properties

当然,我们不能删除项目最顶层的application.properties配置,在该文件中,根据部署场景不同,切换不同的配置文件:配置spring.profiles.active,属性值为${profile}

  • spring.profiles.active=dev:启用application-dev.properties
  • spring.profiles.active=test:启用application-test.properties
  • spring.profiles.active=prod:启用application-prod.properties

启动时指定:

在执行有参启动时,可以在命令中进行指定要选用的配置文件,例如:java -jar xx.jar –spring.profiles.active=test这个命令的优先级是最高的,即使application.properties中已经配置spring.profiles.active=dev,最终程序还是会用application-test.properties配置文件。

spring.profiles.include属性

再进一步,对于开发环境,想用不同的配置文件存储开发环境不同的配置,例如:application-dev1.properties中存储jdbc信息application-dev2.properties中存储ip、端口信息即在启用application-dev.properties开发环境(主)配置文件时,同时启用application-dev1.properties和application-dev2.propertie。那么,可以使用spring.profiles.include属性:同时启用其他的profile

配置方法:

  • 若是properties文件:spring.profiles.include=dev1,dev2
  • 若是yaml文件中,spring.profiles.include:-dev1-dev2或者:spring.profiles.include:dev1,dev2

配置的位置和区别:

  • 配置方式一:application.properties中,配置spring.profiles.active=dev的同时指定spring.profiles.include=dev1,dev2
  • 配置方式二:application.properties中,配置spring.profiles.active=dev,application-dev.properties中,配置spring.profiles.include=dev1,dev2。使用application-dev.properties时自动就激活了dev1、dev2两个文件,不用再次指定。(个人认为第二种方法更好)

区别:

  • 第一种方式启动时,控制台打印The following profiles are active:dev1,dev2,dev
  • 第二种方式启动时,控制台打印The following profiles are active:dev, dev1,dev2按照顺序,后面的覆盖前面的。

用示例来使用和区分

在某个工程里,我依据开发、生产环境,以及它们要同时激活的配置,创建了多个配置,分别如下:application-dev.properties配置:

my.name=dev
my.height=170

#同时触发dev1和dev2的激活
spring.profiles.include=dev1,dev2

application-dev1.properties配置:

my.name=dev1
my.height=171

application-dev2.properties配置:

my.name=dev2
#my.height=172

application-prod.properties配置:

my.name=prod
my.height=180

application-prod1.properties配置:

my.name=prod1
my.height=181

application-prod2.properties配置:

my.name=prod2
my.height=182

测试一:

在application.properties配置文件中,指定

#激活的是开发环境配置文件
spring.profiles.active=dev

启动时不指定参数结果:打印的内容为:姓名:dev2,身高:171原因:

  • 先加载application.properties
  • 再加载application-dev.properties,my.name=devmy.height=170
  • 加载application-dev1.properties,my.name=dev1my.height=171
  • 加载application-dev2.properties,my.name=dev2my.height=171

测试二:

在测试一的基础上,启动时指定参数:–spring.profiles.active=prod优先级最高,会使用生产环境配置文件结果:打印的内容为:姓名:prod,身高:180

测试三:

在application.properties配置文件中,指定

#激活的是生产环境配置文件
spring.profiles.active=prod
#同时触发prod1和prod2的激活
spring.profiles.include=prod1

启动时不指定参数结果:The following profiles are active: prod1,prod2,prod打印的内容为:姓名:prod,身高:180

————————

spring.profiles.active和spring.profiles.include的区别与使用

  • Business scenario
  • Optimization mode
  • spring.profiles.active属性
  • Specify at startup:
  • spring.profiles.include属性
  • Configuration method:
  • Location and difference of configuration:
  • Use examples to use and distinguish
  • Test 1:
  • Test 2:
  • Test 3:

Business scenario

A set of programs may be deployed to different environments. Development, testing and production environments have different configuration information, including JDBC address, IP, port, etc. If you change the configuration every time you deploy in the same configuration file, it will be very troublesome and error prone.

Optimization mode

spring.profiles.active属性

A good way is to < strong > create different configuration files < / strong >, and the naming rules follow application – ${profile}. Properties, for example:

  • 开发环境配置文件:application-dev.properties
  • 测试环境配置文件:application-test.properties
  • 生产环境配置文件:application-prod.properties

Of course, we cannot delete the application.properties configuration at the top level of the project. In this file, < strong > switch different configuration files according to different deployment scenarios: < strong > configure spring.profiles.active, and the attribute value is ${profile} < / strong >.

  • spring.profiles.active=dev:启用application-dev.properties
  • spring.profiles.active=test:启用application-test.properties
  • spring.profiles.active=prod:启用application-prod.properties

Specify at startup:

When executing < strong > parametric startup < / strong >, you can specify the configuration file to be selected in the command, for example: Java – jar XX. Jar — spring. Profiles. Active = test. This command has the highest priority. Even if spring. Profiles. Active = dev is configured in application.properties, the final program will use the application-test.properties configuration file.

spring.profiles.include属性

Further, for the development environment, you want to < strong > use different configuration files to store different configurations of the development environment < / strong >, for example, JDBC information is stored in application-dev1.properties, and IP and port information is stored in application-dev2.properties, that is, when the application-dev.properties development environment (main) configuration file is enabled, < strong > enable both < / strong > application-dev1.properties and application-dev2.properties. Then, you can use the < strong > spring.profiles.include attribute to enable other profiles at the same time < / strong >

Configuration method:

  • 若是properties文件:spring.profiles.include=dev1,dev2
  • 若是yaml文件中,spring.profiles.include:-dev1-dev2或者:spring.profiles.include:dev1,dev2

Location and difference of configuration:

  • 配置方式一:application.properties中,配置spring.profiles.active=dev的同时指定spring.profiles.include=dev1,dev2
  • 配置方式二:application.properties中,配置spring.profiles.active=dev,application-dev.properties中,配置spring.profiles.include=dev1,dev2。使用application-dev.properties时自动就激活了dev1、dev2两个文件,不用再次指定。(个人认为第二种方法更好)

< strong > difference: < / strong >

  • 第一种方式启动时,控制台打印The following profiles are active:dev1,dev2,dev
  • 第二种方式启动时,控制台打印The following profiles are active:dev, dev1,dev2按照顺序,后面的覆盖前面的。

Use examples to use and distinguish

In a project, I created multiple configurations according to the development and production environments and the configurations they want to activate at the same time, as follows: application-dev.properties configuration:

my.name=dev
my.height=170

#同时触发dev1和dev2的激活
spring.profiles.include=dev1,dev2

application-dev1.properties配置:

my.name=dev1
my.height=171

application-dev2.properties配置:

my.name=dev2
#my.height=172

application-prod.properties配置:

my.name=prod
my.height=180

application-prod1.properties配置:

my.name=prod1
my.height=181

application-prod2.properties配置:

my.name=prod2
my.height=182

Test 1:

In the application.properties configuration file, specify

#激活的是开发环境配置文件
spring.profiles.active=dev

No parameters are specified during startup. Result: the printed content is: Name: dev2, height: 171 reason:

  • 先加载application.properties
  • 再加载application-dev.properties,my.name=devmy.height=170
  • 加载application-dev1.properties,my.name=dev1my.height=171
  • 加载application-dev2.properties,my.name=dev2my.height=171

Test 2:

On the basis of test 1, specify the parameters during startup: – spring. Profiles. Active = prod has the highest priority, and the production environment configuration file will be used. Results: the printed contents are: Name: prod, height: 180

Test 3:

In the application.properties configuration file, specify

#激活的是生产环境配置文件
spring.profiles.active=prod
#同时触发prod1和prod2的激活
spring.profiles.include=prod1

No parameters are specified during startup. Results: the following profiles are active: prod1, prod2, prod. the printed contents are: Name: prod, height: 180