70

Is there a way that a context can be loaded using web.xml in a Spring MVC application?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ganesh
  • 1,654
  • 2
  • 19
  • 32

3 Answers3

121

From the spring docs

Spring can be easily integrated into any Java-based web framework. 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.

The <context-param>:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

<listener>
   <listener-class>
        org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener> 

You can then use the WebApplicationContext to get a handle on your beans.

WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
SomeBean someBean = (SomeBean) ctx.getBean("someBean");

See http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/context/support/WebApplicationContextUtils.html for more info

ddewaele
  • 22,363
  • 10
  • 69
  • 82
  • 2
    How to access the context? and Are you saying that context will be loaded with Spring Context as soon as the application start? Please clarify because I am new to Spring,. Thanks for you response – Ganesh Jun 23 '11 at 08:43
  • Here is the link for latest APIs related to WebApplicationContextUtils. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/support/WebApplicationContextUtils.html – Ajitesh Feb 23 '18 at 05:44
34

You can also specify context location relatively to current classpath, which may be preferable

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
divanov
  • 6,173
  • 3
  • 32
  • 51
fmucar
  • 14,361
  • 2
  • 45
  • 50
  • What is the significance of the `*`? It doesn't work without it: `IOException parsing XML document from ServletContext resource [/>classpath:/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/>classpath:/applicationContext.xml]` – DavidS Oct 02 '17 at 18:32
  • 1
    I just found a blog post answering my question about `classpath*` [here](http://www.gridshore.nl/2008/05/13/spring-application-context-loading-tricks/). – DavidS Oct 02 '17 at 18:54
17

You can also load the context while defining the servlet itself (WebApplicationContext)

  <servlet>
    <servlet-name>admin</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
                /WEB-INF/spring/*.xml
            </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>admin</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

rather than (ApplicationContext)

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

<listener>
   <listener-class>
        org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener> 

or can do both together.

Drawback of just using WebApplicationContext is that it will load context only for this particular Spring entry point (DispatcherServlet) where as with above mentioned methods context will be loaded for multiple entry points (Eg. Webservice Servlet, REST servlet etc)

Context loaded by ContextLoaderListener will infact be a parent context to that loaded specifically for DisplacherServlet . So basically you can load all your business service, data access or repository beans in application context and separate out your controller, view resolver beans to WebApplicationContext.

Community
  • 1
  • 1
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289