【问题标题】:Read property file outside war using spring使用弹簧读取战争之外的属性文件
【发布时间】:2011-12-16 03:46:58
【问题描述】:

enter code here我有一个属性文件放在 etc 文件夹中。每个子模块中的“myapplication.properties”和其他一些属性文件..我正在尝试执行以下操作

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="searchContextAttributes" value="true"/>
    <property name="contextOverride" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:application.properties</value> 
            <value>${config}</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean> 

我正在尝试 mvn -Dconfig=~/my.properties jetty:run

属性是从 application.properties 读取的,但不是从 config.. 读取的。

在运行应用程序时,我得到 ${jdbc.url} 不正确.. 此 url 存在于 my.properties 中.. 如何实现?

谢谢

【问题讨论】:

    标签: spring spring-mvc properties


    【解决方案1】:

    我认为这个特性在 Spring 3.1 中可以通过新的 Environment 抽象来使用。详情见以下春季博客:

    http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/.

    如果 spring 3.1 不是一个选项,您可以将 spring xml 配置文件中的文件名和路径硬编码到某个众所周知的位置,然后开发人员可以对其进行符号链接。

    【讨论】:

      【解决方案2】:

      这就是我所拥有的,运行它

      <bean id="placeholderConfigConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="systemPropertiesModeName">
              <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
          </property>
          <property name="ignoreUnresolvablePlaceholders">
              <value>true</value>
          </property>
      </bean>
      
      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="location" value="file:${config}" />
      </bean>
      

      并在 MAVEN_OPTS 中添加 -Dconfig=/var//my.properties 并执行 mvn jetty:run

      我发现的另一种解决方案.. 而不是做详细的配置

       <context:property-placeholder location="file:${config}"/> 
      

      【讨论】:

        猜你喜欢
        • 2011-09-06
        • 1970-01-01
        • 2014-06-04
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 2011-03-13
        • 1970-01-01
        相关资源
        最近更新 更多