【问题标题】:Can one Spring PropertyPlaceholderConfigurer configure another one?一个 Spring PropertyPlaceholderConfigurer 可以配置另一个吗?
【发布时间】:2012-03-30 23:22:57
【问题描述】:

我有一个这样的 PropertyPlaceholderConfigurer:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:assuredlabor/margarita-${runningMode}.properties</value>
        </list>
    </property>
</bean>

我希望能够像这样在 web.xml 中指定我的运行模式:

<context-param>
    <param-name>runningMode</param-name>
    <param-value>production</param-value>
</context-param>

所以我把这个bean放在我上面描述的'main'属性bean之上:

<bean id="servletPropertyPlaceholderConfigurer" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
</bean>

但这似乎不起作用。

Spring 可以做到这一点吗?我现在使用的是 2.5 版。

我发现了这个类似的问题:

PropertyPlaceholderConfigurer with Tomcat & ContextLoaderListener

但是没有讨论 ServletContextPropertyPlaceholderConfigurer,所以我认为这是一个合理的问题。

【问题讨论】:

    标签: java spring servlets


    【解决方案1】:

    你不能在 spring 2 中做到这一点,我不认为没有一些自定义编码,因为一个属性占位符不能配置另一个。

    您需要使用 spring 3 来开箱即用。为此,您必须创建一个以某种方式具有所需值的 bean,并在设置属性占位符时使用 spring-el 来引用该 spring。有一个特殊的 bean 用于获取单个 servlet 上下文参数,如下所示:

    <bean id="runningMode" class="org.springframework.web.context.support.ServletContextAttributeFactoryBean">
      <property name="attributeName" value="runningMode" />
    </bean>
    
    <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:assuredlabor/margarita-#{runningMode}.properties</value>
            </list>
        </property>
    </bean>
    

    然后你就可以在正常的 ${} 语法中引用任何属性

    【讨论】:

      【解决方案2】:

      来自source code

      PropertyPlaceholderConfigurer 的子类,将占位符解析为 ServletContext 初始化参数(即 web.xml 上下文参数条目)。

      除了 web.xml 上下文参数之外,还可以与“位置”和/或“属性”值组合。或者,可以在没有本地属性的情况下定义,以将所有占位符解析为 web.xml 上下文参数(或 JVM 系统属性)。

      如果无法针对应用程序中提供的本地属性解析占位符,则此配置器将回退到 ServletContext 参数。也可以配置为让 ServletContext 初始化参数覆盖本地属性(contextOverride=true)。

      可选地支持搜索 ServletContext 属性:如果打开,否则无法解析的占位符将与相应的 ServletContext 属性匹配,如果找到则使用其字符串化值。这可用于将动态值提供给 Spring 的占位符解析。

      如果不在 WebApplicationContext(或任何其他能够满足 ServletContextAware 回调的上下文)中运行,该类的行为将类似于默认的 PropertyPlaceholderConfigurer。这允许在测试套件中保留 ServletContextPropertyPlaceholderConfigurer 定义。

      据我了解,这意味着您可以只使用一个配置器:

      <bean id="propertyPlaceholderConfigurer" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
          <property name="ignoreUnresolvablePlaceholders" value="true"/>
          <property name="locations">
              <list>
                  <value>classpath:assuredlabor/margarita-${runningMode}.properties</value>
              </list>
          </property>
      </bean>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-30
        • 2011-12-26
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多