Tuesday, October 29, 2013

What is the difference between Application Context and Web Application Context?
I am aware that WebApplicationContext is used for Spring MVC architecture oriented applications?
I want to know what is the use of ApplicationContext in MVC applications? And what kind of beans are defined in ApplicationContext?
share|improve this question
 
I do not believe that is is a duplicate of stackoverflow.com/questions/3652090/… That question asks about the content of the web.xml file; this question is asking about some Spring classes. –  Raedwald Jul 5 at 10:41
 
@Raedwald that's not true. The other question is not talking about web.xml but it's talking about the Spring XML bean configuration variants of ApplicationContext and WebApplicationContext. All bean definitions in applicationContext.xml will be available in the ApplicationContext while all bean definitions in *-servlet.xml will be available in a WebApplicationContext. –  Dimitri M Jul 5 at 12:39 
up vote36down voteaccepted
Web Application context extended Application Context which is designed for work with the standardjavax.servlet.ServletContext so it's able to communicate with the container.
public interface WebApplicationContext extends ApplicationContext {
    ServletContext getServletContext();
}
Beans, instantiated in WebApplicationContext will also be able to use ServletContext if they implement ServletContextAware interface
package org.springframework.web.context;
public interface ServletContextAware extends Aware { 
     void setServletContext(ServletContext servletContext);
}
There many things possible to do with the ServletContext instance, for example accessing WEB-INF resources(xml configs and etc.) by calling the getResourceAsStream() method. Typically all application contexts defined in web.xml in a servlet Spring application are Web Application contexts, this goes both to the root webapp context and the servlet's app context.
Also, depending on web application context capabilities may make your application a little harder to test, and you may need to use MockServletContext class for testing.
Difference between servlet and root context Spring allows you to build multilevel application context hierarchies, so the required bean will be fetched from the parent context if it's not present in the current aplication context. In web apps as default there are two hierarchy levels, root and servlet contexts:enter image description here
such thing allows you to run some services as the singletons for the entire application(Spring Security beans and basic database access services typically reside here) and another as separated services in the corresponding servlets to avoid name clashes between beans. For example one servlet context will be serving the web pages and another will be implementing a stateless web service.
This two level separation comes out of the box when you use the spring servlet classes: to configure the root application context you should use context-param tag in your web.xml

    contextConfigLocation
/WEB-INF/root-context.xml /WEB-INF/applicationContext-security.xml
(the root application context is created by ContextLoaderListener which is declared in web.xml

        org.springframework.web.context.ContextLoaderListener
) and servlet tag for the sevlet application contexts

   myservlet
org.springframework.web.servlet.DispatcherServlet contextConfigLocation app-servlet.xml
please note that if init-param will be omitted, then spring will use myservlet-servlet.xml in this example.
share|improve this answer

No comments: