【发布时间】:2021-09-30 12:38:03
【问题描述】:
在我的应用程序中,我有这些属性文件:
应用程序.properties
应用程序-prod.properties
里面我有同样的属性
spring.datasource.password=my-dev-password #为默认的
spring.datasource.password=${PROD_DATABASE_PASSWORD} #用于产品文件
在服务器上我运行我的应用程序:
java -jar "myjar.jar" --spring.profiles.active=prod
到目前为止一切正常。
现在我想使用一个额外的文件来覆盖服务器上的相同属性,例如:
java -jar myjar.jar --spring.profiles.active=prod --spring.config.additional-location=file:/to/folder/application.properties
但它没有工作!
我尝试将它作为 java 属性传递,但它也不起作用!
java -Dspring.config.additional-location=file:/to/folder/application.properties -jar myjar.jar --spring.profiles.active=prod
我在这里错过了什么?
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
我正在使用 spring-boot 2.3.5
更新
当我只引用它工作的文件夹时:
--spring.config.additional-location=file:/to/folder/
我认为它只需要与spring.config.location 相反的文件夹,但是当我查看代码时,两者都在ConfigFileApplicationListener 中加载了相同的代码:
private Set<String> getSearchLocations() {
Set<String> locations = getSearchLocations(CONFIG_ADDITIONAL_LOCATION_PROPERTY);
if (this.environment.containsProperty(CONFIG_LOCATION_PROPERTY)) {
locations.addAll(getSearchLocations(CONFIG_LOCATION_PROPERTY));
}
else {
locations.addAll(
asResolvedSet(ConfigFileApplicationListener.this.searchLocations, DEFAULT_SEARCH_LOCATIONS));
}
return locations;
}
【问题讨论】:
-
当你使用更新后的配置 --spring.config.additional-location=file:/to/folder/.我相信它仍然会从默认的道具加载你的道具,而不是从存在于附加位置的文件中加载?
-
是的
spring.config.additional-location覆盖 jar 中的application.properties -
我用我的 Spring Boot 2.3.4 应用程序玩了一段时间。它对我有用。你可以尝试 ro debug 的东西。将 spring.config.additional-location 中的文件重命名为 other.properties(或与 Spring 默认值不同的名称),并尝试这是否有影响。仔细检查,如果您使用的是正确的文件路径。
标签: java spring spring-boot spring-profiles spring-properties