Thursday, July 19, 2018

ApplicationContext of Spring

Context
Context is the larger surrounding part which can have influence on the current unit of work. It can be the running environment, the environment variables, instance variables, local variables, state of other classes etc. For example, ServletContext represents the servlet’s environment within servlet container. Similarly, ApplicationContext represents Spring’s environment within Spring container. More examples of context are JSF's FacesContext, Android's Context, JNDI's InitialContext etc. The context implementation usually follow the Facade Pattern which hides the complexities of the larger system and provides a simpler interface to the client.

ApplicationContext
The ApplicationContext is the central interface of Spring application. It provides configuration information to the application. It is read-only at run time, but can be reloaded if necessary. A number of classes implement the ApplicationContext interface, allowing for a variety of configuration options and types of applications.

ApplicationContext is the context initialized by ContextLoaderListener that we define in our application’s web.xml file. There will be only one application context per web application. We can explicitly declare the context configuration file name in web.xml using the contextConfigLocation param. Otherwise, Spring will search for the file applicationContext.xml under WEB-INF folder. The configuration would look something like this:
 
<listener>    
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
In the above configuration, we are using the file root-context.xml and Spring will create an ApplicationContext from it.

WebApplicationContext
WebApplicationContext is the servlet specific context. Each DispatcherServlet is associated with a single WebApplicationContext. Dispatcher servlets are also configured in the application’s web.xml file. Each dispatcher servlet can be initialized from <servlet-name>-servlet.xml file, which is the default file name pattern.  Multiple DispatcherServlets allow us to categorize the incoming requests based on the url-pattern of servlet and handle them accordingly. For example, one dispatcher servlet can help serving the web pages via Controller, while another servlet can be used to implement a stateless REST web service.
To change the configuration file of dispatcher-servlet, we can use init-param with contextConfigLocation as param-name, like the following:
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/sample-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
    
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
In the above configuration, Spring will not find dispatcher-servlet context file with the name mvc-dispatcher-servlet.xml, but it will look for the file name sample-dispatcher-servlet.xml because it is specified as the init-param value for contextConfigLocation

Difference between ApplicationContext and WebApplicationContext
ApplicationContext is the root-context, which has bean configurations that can be used across the entire application as singletons. There is always a single application context in a web application. But, there can be multiple WebApplicationContexts for each of the dispatcher servlets that we specify in application’s web.xml.
It’s always better to keep a clear separation between middle-tier services and web-related components.  Middle-tier services like business logic components and data access classes are preferred to be defined in the ApplicationContext. Web-related components such as controllers and view-resolvers are preferred to be defined in the respective dispatcher-servlet‘s WebApplicationContext.

References 
http://www.jcombat.com/spring/applicationcontext-webapplicationcontext
https://spring.io/understanding/application-context
http://codesenior.com/en/tutorial/Spring-ContextLoaderListener-And-DispatcherServlet-Concepts
https://schoudari.wordpress.com/2012/07/23/purpose-of-contextloaderlistener-spring-mvc/
https://stackoverflow.com/questions/11815339/role-purpose-of-contextloaderlistener-in-spring