【问题标题】:Spring application context : access web.xml context-params?Spring 应用程序上下文:访问 web.xml 上下文参数?
【发布时间】:2010-02-01 08:43:16
【问题描述】:

你好,

有没有办法从 web.xml 上下文参数中获取值到 Spring 上下文中?

例如,我将 web.xml 中的值定义为:

<context-param>
  <param-name>compass-index</param-name>
  <param-value>file:///home/compass/index</param-value>
</context-param>

我想将该值分配给 bean-property:

<bean ...>
<props>
  <prop key="compass.engine.connection">
    ${from web.xml context-param?}
  </prop>
</props>
</bean>

提前致谢?

【问题讨论】:

    标签: java spring web.xml


    【解决方案1】:

    是的 - ServletContextPropertyPlaceholderConfigurer

    This article 解释细节。简而言之,您需要:

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

    然后使用如下属性:

    <bean ...>
       <property name="compassIndex" value="${compass-index}" />
    </bean>
    

    @Value("${compass-index}")

    【讨论】:

    • 从 Spring 3.1 开始,类 ServletContextPropertyPlaceholderConfigurer 已弃用(有关详细信息,请参阅 javadocs)。
    • 谢谢。因此,请查看static.springsource.org/spring/docs/3.1.x/javadoc-api/org/… 处的弃用说明
    • Spring 文档中特别“酷”的特性是已经提到弃用,但没有提供更新的示例代码。
    • @Askar Kalkov 文档不是很清楚你必须做什么,但最后它实际上很简单。看到这个答案:stackoverflow.com/a/21175824/1669464
    • 这似乎是正确的 XML,因为 ServletContextPropertyPlaceholderConfigurer 已被弃用:
    猜你喜欢
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    相关资源
    最近更新 更多