Sep
11
2015

what is Application context level hierarchy in spring web application

Hi in this article of spring we will see what is applicationcontext , what is applicationcontext level hierarchy in spring web application, Why we need this hierarchy level, how applicationcontext is configure in spring web application etc. It is mostly theoretical article but it is very useful to understand spring application architecture.Now let begin with what is applicationcontext.

What is applicationcontext


Applicationcontext is container where beans live. In spring application we define beans most often in config files. Now for these bean to get instantiated a spring application context instance is created and  the config files in which bean configuration is given is loaded. Spring application context parse these files and  instantiate bean. When it is creating beans it store beans in Map. On completion of bean creation Map has all beans that are specified in config file. So application context act as a container for beans.

Why need applicationcontext hierarchy


When we are working with spring web application then we provide applicationcontext in two level.

  1. root applicationcontext(applicationContext)
  2. servlet specific applicationcontext(Webapplicationcontext)

Note: If your application is small than there is no need to create applicationcontext hierarchy. You can place all beans within a single config file which is your spring-serlvet.xml file.

But if your application is of large size and there are lot of beans in different module we should always use hierarchy level of context. The reason behind this hierarchy is that some of bean may be used throughout application i.e inside other beans  for example some service bean lets say userService may be used in many controllers of different modules.

Other beans that are used within specific context. For example let say we have a web application and there are many modules lets say student,  faculty and admin and so on and there is no relationship between beans of there modules. So beans for student can be defined in spring-servlet1 and beans for faculty can be defined in spring-servlet2 and so on. 

The reason for defining bean of these module in different spring-servlet is mapping of HTTP request, code clean-ness and remove confusion. Let say  HTTP request for student <url-pattern>/student/*</url-pattern> should be mapped in Controllers of student module. So we can configure a seprate spring-servlet.xml for it and can define controller of student module in it. Let say if we want to add or modify or remove any bean in a stuent module then we can do it easily in spring-servlet.xml file

If we put all beans of all module in a single spring-servlet file then if we want to modify any beanthen it is tedious to find that particular bean out of lots of beans in same file. Although there is no harm in putting all bean in single file but when the project size grow then it became difficult to manage.

Now let come to our main question why do we need hierarchy of application context. As we have seen above that beans related to particular module should be placed in its related spring-servlet file.

Now let suppose there are some beans like dao, service and spring security bean that are common in all modules. So we may defile these bean separately in some other file so that they can be share in all module.
For this purpose we may put these bean inside root application context. Root application context is shared in
all serlvet specific application context.

The root application context is parent of all servlet specific application context and is created prior to servlet specific application context.

We put all web-specific configuration like Handler Mappings etc in servlet specific application context and all non web-specific configuration like service dao and spring security concern etc in root application context.

root application context is created by ContextLoaderListener which is declared in web.xml

How applicationcontext is configure in spring web application


In above section we have seen why applicationcontext hierarchy is important if application size is large. Now let see how to configure this application context in spring web application. As we have seen there are two level of application context.

  1. root application context
  2. servlet specific application context

To configure root level application context we use ContextLoaderListener. ContextLoaderListener create a spring container that is shared between all servlet and filters. It look for context-param element in web.xml.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-config/applicationContext-hibernate.xml
</param-value>
</context-param>

The context-param element contains the declaration of a web application’s servlet context initialization parameters. If context-param is not present in  web.xml then it will look for default applicationcontext.xml under Web-INF folder and if applicationcontext.xml present then it create beans by parsing config file. If it does not found applicationContext.xml cofig file under Web-INF then it will throw FileNotFoundException.

If context-param elemnt is present then it pick xml configuration file available in param-value element of context-param element. Like in above it will look for config file applicationContext -hibernate.xml in classpath and instantiate bean defined in this file and put in container.

ContextLoaderListener implement destroy method that will destroy beans when server shutdown.  Bootstrap listener to start up and shut down Spring’s root WebApplicationContext.

Now let’s see how to configure servlet specific context.

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

Here bean defined in spring-servlet-context.xml will be registred in servlet-specific application context. Spring will  parse spring- servlet- context.xml and register bean defined in this config file. If <init-param> is not used to declare config file then spring will look spring-servlet.xml file under Web-INF folder.

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework .web.servlet. DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

This is servlet name followed by servlet postfix. So here spring will look for spring-servlet.xml config file in Web-INF file.

We can also inject bean defined in root application context in servlet specific context like this

<bean id=”studentController” class=”com. javafreakers. StudentController>
<property name=”studentService” ref=”studentService”/> <!–studentService is defined in applicationcontext–>
</bean>

Now let see how to declare multiple servlet specific context.

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

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

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

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

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

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

Here are three servlet specific context.

This is all in this tutorial. hope it has helped you in understanding  application context and webapplication context. I will come up with some more tutorial very soon. Take care and Enjoy.

About the Author: devender kumar

java/j2EE developer

Comments are closed.