15

Greetings ,

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

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

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

Thanks in advance?

Ashika Umanga Umagiliya
  • 8,988
  • 28
  • 102
  • 185

1 Answers1

25

Yes - ServletContextPropertyPlaceholderConfigurer

This article explains 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}")

Paul
  • 19,704
  • 14
  • 78
  • 96
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 5
    As of Spring 3.1 class ServletContextPropertyPlaceholderConfigurer is deprecated (see javadocs for details). – Petr Gladkikh Aug 23 '12 at 09:06
  • Thanks. So look at the deprecation instructions at http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/context/support/ServletContextPropertyPlaceholderConfigurer.html – Bozho Aug 23 '12 at 09:56
  • 2
    Especially "cool" feature in spring docs is the fact that deprecation has been mentioned, but no updated sample code has been provided. – Askar Kalykov Sep 19 '13 at 05:28
  • @Askar Kalkov The documentation is not very clear on what you have to do, but in the end it's actually quite simple. See this answer: http://stackoverflow.com/a/21175824/1669464 – Pytry Jan 17 '14 at 23:27
  • 3
    This seems like the correct XML since ServletContextPropertyPlaceholderConfigurer was deprecated: – Anthony Hayward Jul 10 '15 at 11:06