Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 823
Ashika Umanga Umagiliya Avatar asked Feb 01 '10 08:02

Ashika Umanga Umagiliya


People also ask

What is context param in web xml Spring?

In a spring web application, contextConfigLocation context param gives the location of the root context. Your config is strange, for a spring-mvc application, because by default, servletname-servlet. xml (where servletname is the name of a DispatcherServlet servlet) is the child application context for the servlet.

What is application context in web xml?

All you need to do is to declare the ContextLoaderListener in your web. xml and use a contextConfigLocation to set which context files to load. You can then use the WebApplicationContext to get a handle on your beans. Here is the link for latest APIs related to WebApplicationContextUtils.

What is the difference between DispatcherServlet and ContextLoaderListener in Spring?

Whereas DispatcherServlet is expected to load beans containing web components such as controllers, view resolvers, and handler mappings, ContextLoaderListener is expected to load the other beans in your application.


1 Answers

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

like image 124
Bozho Avatar answered Oct 06 '22 04:10

Bozho