外部化application.properties

时间:2019-04-23 10:41:39

标签: spring tomcat

我有一个带有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;
}

上一个问题中没有提供以前的解决方案。 我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

在这里您可以找到有关使用外部配置文件的提示: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")};