WebApplicationContext and XmlWebApplicationContext(applicationContexts used in a Web Applications)


In non-web applications we use ApplicationContext interface to provide beans configuration to our applications from xml files.For example:


ApplicationContext context = new FileSystemXmlApplicationContext("c:/foo.xml");


OR


ApplicationContext context = new ClassPathXmlApplicationContext("foo.xml");


Here we are using two implementations of ApplicationContext interface namely FileSystemXmlApplicationContext and ClassPathXmlApplicationContext for loading configuration from xml files.


But in web applications we use WebApplicationContext interface instead of ApplicationContext interface to provide bean configuration to our web application and to represent the spring container.


WebApplicationContext extends ApplicationContext and has some extra features necessary for web applications.


In web applications, application contexts are provided at two levels(Context Hierarchy):

1)Root application context
2)Application context specific to each servlet defined in web.xml

There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.

Both contexts are represented by WebApplicationContext interface.

Each servlet specific application context inherits the configuration defined in root application context.Thus root application context is shared among all servlet specific application contexts.

 
1)Root application context:

Root application context generally contains configuration information from more than one configuration files representing different layers of application like db layer,service layer etc. Beans specific to each layer should be defined in separate configuration file.For example data access beans should reside in application-context-db.xml, service layer beans should be in application-context-service.xml

It is loaded by using ContextLoaderListener class (org.springframework.web.context.ContextLoaderListener) declared in web.xml file as.

web.xml:


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context-db.xml
/WEB-INF/applicationContext.xml
/WEB-INF/application-context-service.xml
</param-value>
</context-param>

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

ContextLoaderListener class has two context-param level parameters in web.xml:
1) contextClass  and
2) contextConfigLocation  (shown in above code)
  
contextConfigLocation:
ContextLoaderListener reads the configuration files as defined in contextConfigLocation parameter value.

In above code root application context contains configuration information from three files namely-application-context-db.xml, applicationContext.xml and application-context-service.xml.

If contextConfigLocation parameter is not defined in web.xml then by default ContextLoaderListener will read configuration from  /WEB-INF/applicationContext.xml.

contextClass:
contextClass parameter is used to specify the class which can be used to represent  application context/spring container for our web application. Any class that implements WebApplicationContext interface can be used as value for this parameter. 

If this parameter is not defined in web.xml as in above code, then by default org.springframework.web.context.support.XmlWebApplicationContext class (an implementation of WebApplicationContext interface) is used.

We can change value of this parameter as: 

<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>

AnnotationConfigWebApplicationContext is also an implementation of WebApplicationContext interface.

ContextLoaderlistener delegates this information to org.springframework.web.context.ContextLoader class in order to create root application context.


Once the context files specified in contextConfigLocation  parameter get loaded, Spring creates a WebApplicationContext object (by default XmlWebApplicationContext object) based on the bean definitions and stores it in the ServletContext of the webapplication.

We can obtain this root WebApplicationContext object from ServletContext by using org.springframework.web.context.support.WebApplicationContextUtils class as:


WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);


Now we can use this WebApplicationContext object to obtain beans in our web application as:

MyBean beanObject= (MyBean) ctx.getBean("mybeanId");

getBean() here is inhertited by WebApplicationContext by BeanFactory interface.

2)Servlet Specific Application Context:

Each servlet defined in web.xml has its own  application context  which inherits all configurations defined in root application context. For servlet-specfic contexts,by default configurations are looked up in a file named as /WEB-INF/[servlet-name]-servlet.xml.

Thus two different servlets named servlet1 and servlet2 defined in web.xml will have their separate non-shared configurations (local to their servlet contexts) loaded by default from the files /WEB-INF/servlet1-servlet.xml and /WEB-INF/servlet2-servlet.xml.

For example, suppose we have defined a dispatcherservlet in web.xml as: 

web.xml:



<web-app>
<servlet>
<servlet-name>mydispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mydispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>



Then spring will look for a file named as /WEB-INF/mydispatcher-servlet.xml for bean configurations.

In case of DispactherServlet, there are also two context-param level parameters:
1) contextClass  and
2) contextConfigLocation

They are defined for the same puropse as in ContextLoaderListener described above.




I would like to know your comments and if you liked the article then please share it on social networking buttons.


12 comments:

  1. good blog,truely helpful...

    ReplyDelete
  2. This is a very helpful article for a spring noob like me. Thanks.

    ReplyDelete
  3. It made me understand the how Spring works on WebApplication context.
    Truely recommended Article. Appreciate it.
    Will like to have some exposure on Servlet specific application context.
    How does the request mapping works i.e. like a request handled in servlet.xml file.

    ReplyDelete
  4. Nice Useful & Detailed Article.

    ReplyDelete
  5. easy understandable

    ReplyDelete
  6. very clean explanation. understandable...

    ReplyDelete