java Spring 应用程序上下文:访问 web.xml 上下文参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2175502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:52:28  来源:igfitidea点击:

Spring application context : access web.xml context-params?

javaspringweb.xml

提问by Ashika Umanga Umagiliya

Greetings ,

问候 ,

Is there any way to get values from web.xml context-param into Spring context?

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

For example I define the value in web.xml as :

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

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

And I want to assign that value to the bean-property as:

我想将该值分配给 bean 属性:

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

Thanks in advance?

提前致谢?

回答by Bozho

Yes - ServletContextPropertyPlaceholderConfigurer

是的 - ServletContextPropertyPlaceholderConfigurer

This articleexplains the details. In short, you need:

这篇文章解释了细节。简而言之,您需要:

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

and then use the properties like:

然后使用如下属性:

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

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

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