相关疑难解决方法(0)

Spring Boot和多个外部配置文件

我有多个属性文件,我想从类路径加载.有一个默认设置/src/main/resources属于myapp.jar.我springcontext希望文件在类路径上.即

<util:properties id="Job1Props"
    location="classpath:job1.properties"></util:properties>

<util:properties id="Job2Props"
    location="classpath:job2.properties"></util:properties>
Run Code Online (Sandbox Code Playgroud)

我还需要使用外部集覆盖这些属性的选项.我有一个外部配置文件夹cwd.根据spring boot doc配置文件夹应该在classpath上.但是从doc中不清楚它是否只会覆盖applicaiton.propertiesconfig中的那个或所有属性.

当我测试它时,只会application.properties被拾取,其余的属性仍然从中获取/src/main/resources.我已经尝试将它们作为逗号分隔列表提供给spring.config.location但是默认设置仍然没有被覆盖.

如何使多个外部配置文件覆盖默认配置文件?

作为我目前使用的解决方法app.config.location(app特定属性),我通过命令行提供.即

java -jar myapp.jar app.config.location=file:./config
Run Code Online (Sandbox Code Playgroud)

然后我改变applicationcontext

<util:properties id="Job2Props"
    location="{app.config.location}/job2.properties"></util:properties>
Run Code Online (Sandbox Code Playgroud)

这就是我在加载Application时在文件和类路径之间进行分离的方法.
EDITS:

//psuedo code

if (StringUtils.isBlank(app.config.location)) {
            System.setProperty(APP_CONFIG_LOCATION, "classpath:");
}
Run Code Online (Sandbox Code Playgroud)

我真的不想使用上面的解决方法,并让spring覆盖类路径上的所有外部配置文件,就像它对application.properties文件所做的那样.

java spring config spring-boot

110
推荐指数
8
解决办法
34万
查看次数

标签 统计

config ×1

java ×1

spring ×1

spring-boot ×1