Spring boot加载多个属性文件

问题描述 投票:0回答:3

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

application.properties

sources=a_source,b_source,c_source

a_source,b_source和c_source具有相同的变量。

啊_source.properties

location = locationb

不_source.properties

location = locationb

从_source.properties

location=locationc

因此,如果我将属性文件的名称添加到application.properties中说d_source,那么应该由应用程序自动加载d_source.properties。这样我的应用程序就可以处理来自4个属性文件的数据。春季靴子可以吗?

java spring spring-boot properties properties-file
3个回答
0
投票

如果您尝试将多个应用程序属性视为多个java属性资源,那么您就错了。你不能,也不应该。每个应用程序为每个环境/配置文件定义一个属性/ yaml文件。

但是,如果要将application.properties或yaml视为环境/配置文件,则可以执行以下操作:

例如。,

应用integration_test.yaml:

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

在上面的内容中,您所说的只要您拥有“integration_test”的活动配置文件,还包括“unit_test”和“mock_test”配置文件中的配置。

但请注意,如果在包含的配置文件中定义了相同或重复的属性或配置键,则会覆盖属性值并替换为下一个提到的配置文件。在这种情况下,如果在所有3个配置文件中具有相同的属性键具有不同的值,则mock_test配置文件的值将用作最终值。

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


0
投票

也许你应该考虑使用Profiles。看看这个:Profiles Profile Specific Properties


0
投票

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

application-unit_test.properties

# content of file

application-mock_test.properties

# content of file

application.properties

spring.profile.active = default
spring.profiles.include = unit_test, mock_test
© www.soinside.com 2019 - 2024. All rights reserved.