Springboot @PropertySource与--spring.config.location = / etc / foo /

时间:2017-05-24 13:48:46

标签: java spring-boot configuration

我想完全外化我的配置文件,以便每个* .properties文件都在/ etc / foo /中。

但我不希望一个大的application.properties中的所有内容,所以我将某些部分拆分为单独的* .properties文件。

我如何告诉@PropertySource它应该解析给定属性文件的位置,就像它对application.properties所做的那样,即在我用“--spring”指定的每个直接(可能是多个)中搜索它.config.location“?

理想情况下,我的班级会这样:

@Configuration
@PropertySource("dpuProfiles.properties")
@ConfigurationProperties("dpu.profiles")
public class DpuProfilesConfiguration {
...
}

如果我明确指定“file:/etc/foo/dpuProfiles.properties”,它确实有效。 我无法获得“$ {spring.config.location} /dpuProfiles.properties”或类似的替代EL工作。由于变量可能包含多个目录,因此可能没有任何意义。

1 个答案:

答案 0 :(得分:1)

您可以通过在-classpath中指定config(s)目录并将其从classpath指向@PropertySource

来执行此操作

对于你的情况,它将是

CLASSPATH=just_added_as_an_example_jar.jar:/etc/foo/
-classpath "$CLASSPATH"

但您需要更改@PropertySource值,如下所示

@PropertySource({"classpath:dpuProfiles.properties"})
@ConfigurationProperties({"classpath:dpu.profiles"})
  

注意 - 以上示例是从脚本运行。

相关问题