【问题标题】:SPRING MVC - Use an external file as @PropertySourceSPRING MVC - 使用外部文件作为@PropertySource
【发布时间】:2017-01-17 21:25:08
【问题描述】:

我想知道如何使用外部文件在不同的上下文中为我的应用加载一些属性..

我知道我可以在PropertySource 注释中传递一个文件。问题是文件的路径。

我的意思是,它看起来想要文件的完整路径,但不同的环境对Tomcat 目录有不同的路径。

我的问题是: 存储这些文件的正确目录在哪里(它必须在应用程序目录之外,否则在每次部署时它都会被删除)以及如何在我的应用程序中链接它。

例如,现在我在tomcat的/webapps目录下创建了一个/config目录。

我可以在我的 Mac 上以这种方式加载文件:

@PropertySource("file:/Library/Tomcat/webapps/config/db.properties" )

但我的生产应用程序在 Linux 服务器上,显然路径不同...

有任何想法,或者更好的是管理它的最佳实践吗?

谢谢

【问题讨论】:

    标签: java spring spring-mvc tomcat properties-file


    【解决方案1】:

    前段时间我有一个类似的问题要解决,我们所做的是我们提供了服务器启动时属性文件的外部目录路径,如下:

    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.annotation.PropertySources;
    
    @Configuration
    @PropertySources({@PropertySource("${ext.properties.dir:classpath:}/db.properties") })
    public class ApplicationConfig {
    
    }
    

    对于基于 XML 的配置:

    <context:property-placeholder
            location="${ext.properties.dir:classpath:}/db.properties" order="-1"
            ignore-unresolvable="true" ignore-resource-not-found="true" />
    

    并根据环境从application-server/jvm 提供ext.properties.dir 值:

    例如,对于 DEV:

    -Dext.properties.dir=file:/dev/Library/Tomcat/webapps/config/
    

    对于 PROD:

    -Dext.properties.dir=file:/prod/Library/Tomcat/webapps/config/
    

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 2015-05-04
      相关资源
      最近更新 更多