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?
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.
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.
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.
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}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With