【问题标题】:Externalize the application.properties外部化 application.properties
【发布时间】:2019-09-12 12:49:17
【问题描述】:

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

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

【问题讨论】:

    标签: spring tomcat


    【解决方案1】:

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

    更新:

    请检查此 sn-p 是否可以帮助您: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")};
    

    【讨论】:

    • 这是从命令行设置位置的方法。从 tomcat setenv.bat 文件中寻找一些解决方案
    猜你喜欢
    • 2017-10-22
    • 2021-05-17
    • 2016-01-01
    • 2014-08-09
    • 2020-11-16
    • 2015-09-10
    • 2017-05-18
    • 2020-06-22
    • 2018-03-13
    相关资源
    最近更新 更多