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?

1 Answer 1

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}")

Sign up to request clarification or add additional context in comments.

5 Comments

As of Spring 3.1 class ServletContextPropertyPlaceholderConfigurer is deprecated (see javadocs for details).
Thanks. So look at the deprecation instructions at static.springsource.org/spring/docs/3.1.x/javadoc-api/org/…
Especially "cool" feature in spring docs is the fact that deprecation has been mentioned, but no updated sample code has been provided.
@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: stackoverflow.com/a/21175824/1669464
This seems like the correct XML since ServletContextPropertyPlaceholderConfigurer was deprecated: <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="environment"><bean class="org.springframework.web.context.support.StandardServletEnvironment" /></property> </bean>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.