我有一个 demoApp 项目,文件夹中有 application.properties
/src/main/resources
现在我想将 application.properties 存储在某个位置,例如:
C:\Users\lenovo\Desktop\externalResource\resources
我尝试了一些工作,比如在 setenv.bat 文件中设置配置位置,比如
set spring.config.location=C:\Users\lenovo\Desktop\externalResource\resources\
也用于在系统环境中设置路径为:
CONF_DIR = C:\Users\lenovo\Desktop\externalResource\resources\
并尝试使用如下代码访问:
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySource = new PropertySourcesPlaceholderConfigurer();
ClassPathResource[] resource = new ClassPathResource[]{new ClassPathResource("file:${CONF_DIR}/application-external.properties")};
propertySource.setLocations(resource);
propertySource.setIgnoreUnresolvablePlaceholders(true);
return propertySource;
}
上一个问题中提供的以前的解决方案都不适合我。 我在这里缺少什么?
最佳答案
在这里你可以找到一些关于使用外部配置文件的技巧:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
例如:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
更新:
请检查此代码段是否可以帮助您:https://stackoverflow.com/a/44442786/974966
此外,如果您想保留当前的实现,请在使用前尝试阅读 ${CONF_DIR}
:
final String confDir = System.getenv("CONF_DIR")
然后,
ClassPathResource[] resource = new ClassPathResource[]{new ClassPathResource("file:"+ confDir +"/application-external.properties")};
关于spring - 外部化 application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55809441/