spring - 外部化 application.properties

标签 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;
}

上一个问题中提供的以前的解决方案都不适合我。 我在这里缺少什么?

最佳答案

在这里你可以找到一些关于使用外部配置文件的技巧: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/

相关文章:

java - Grails 应用程序未部署在 tomcat 中

java - Spring Data Elasticsearch 不会隐式地从注解创建映射

java - Spring Angularjs - 删除方法

java - Apache Tomcat 不包含 conf/Catalina/localhost

Apache Tomcat 在 Google Compute Engine f1-micro 中崩溃

java - Java中同步访问资源(Spring MVC)

java - 无法在Eclipse中启动通过Maven运行的tomcat

java - 如何在远程服务器上部署 struts2 eclipse web 应用程序

apache - 在 Windows 2016 Server 上卸载服务

java - 响应休息分页请求的正确方法