2017-05-24 72 views
0

我想完全外化我的配置文件,以便每个* .properties文件位于/ etc/foo /中。Springboot @PropertySource with --spring.config.location =/etc/foo/

但我不希望一切都在一个大的application.properties中,因此我将某些部分拆分为单独的* .properties文件。

我怎么能告诉@PropertySource,它应该可以解决给定的属性文件,正是因为它会做它的application.properties,即在每一个直接搜索它,我用“指定(可以是多个)的位置 - spring.config.location“?

我班在理想情况下这样看:

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

它的工作,如果我明确指定“文件:/etc/foo/dpuProfiles.properties”。 我无法通过EL工作获得“$ {spring.config.location} /dpuProfiles.properties”或类似的替换。由于该变量可能包含多个目录,因此可能无意义。

回答

1

你可以做到这一点通过指定的配置(一个或多个)目录中的-classpath,并从类路径指向到您的@PropertySource

对于您的情况下,将

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

但是你需要改变所述@PropertySource值如下

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

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

+0

使用/ etc/foo作为classpath目录是个好主意。 我的@ConfigurationProperites是正确的,因为它获取属性名称前缀而不是文件名作为参数。 此外,我在/ pom.xml中将/ etc/foo添加到我的中,以便“mvn test”仍能找到它们。 – lathspell