Spring 引导加载多个属性文件

Spring boot Loading multiple properties files

我有一个 application.properties 指定要加载到应用程序中的属性文件的名称。

application.properties

sources=a_source,b_source,c_source

a_source、b_source和c_source有相同的变量。

a_source.properties

location = locationb

b_source.properties

location = locationb

c_source.properties

location=locationc

因此,如果我将属性文件的名称添加到 application.properties 中,比如 d_source,那么应用程序应该会自动加载 d_source.properties。这样我的应用程序就可以处理 4 个属性文件中的数据。这在 spring 引导中可能吗?

也许您应该考虑使用配置文件。看一下这个: Profiles Profile Specific Properties

如果您试图将多个应用程序属性视为多个 java 属性 资源,那么您就大错特错了。你不能也不应该。每个应用程序为每个 environment/profile.

定义一个 property/yaml 文件

但是,如果您想将 application.properties 或 yaml 视为 environment/profile 基础,您可以执行以下操作:

例如,

申请-integration_test.yaml:

spring.profiles: integration_test
spring.profiles.include:
  - unit_test
  - mock_test

在上面的内容中,您所说的是每当您拥有 "integration_test" 的活动配置文件时,还包括来自 "unit_test" 和 "mock_test" 个配置文件的配置。

But please note that if any same or duplicate properties or config keys defined in included profiles, the properties values are overridden and replaced by the next mentioned profile. In this case if you had same property keys with different values in all 3 profiles, the mock_test profiles' value is used as final value.

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

您也可以使用此解决方案:

申请-unit_test.properties

# content of file

申请-mock_test.properties

# content of file

application.properties

spring.profile.active = default
spring.profiles.include = unit_test, mock_test