ApplicationContext and ServletContext

42,287

Solution 1

Servlet Context:

It is initialized when a Servlet application is deployed. Servlet Context holds all the configurations (init-param, context-params, etc) of the whole servlet application.

Application Context:

It is a Spring specific thing. It is initialized by Spring. It holds all the bean definitions and life-cycle of the beans that are defined inside the spring configuration files. Servlet-Context has no idea about these things.

There are two types of contexts in Spring parent and child.

Spring Parent Context (Application Context / Root Context )

  <listener>
        <listener-lass> 
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/service-context.xml,
            /WEB-INF/dao-context.xml,
            /WEB-INF/was-context.xml,
            /WEB-INF/jndi-context.xml,
            /WEB-INF/json-context.xml
        </param-value>
  </context-param>

role-purpose-of-contextloaderlistener-in-spring
Spring-ContextLoaderListener-And-DispatcherServlet-Concepts
When spring container starts up, it reads all the bean definitions from the configuration files and creates beans objects, and manages the life cycle of the bean objects. This configuration is totally optional.

DispatcherServlet vs ContextLoaderListener
/declaring-spring-bean-in-parent-context-vs-child-context

Spring Child Context ( WebApplicationContext / Child Context )

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

When spring web application starts it will look for spring bean configuration file myWebApplication-servlet.xml. It will read all the bean definitions and create and manages the bean objects' life cycle. If the parent spring context is available it will merge the child spring context with the parent spring context. If there is no Spring parent context available the application will only have the child spring context.

Solution 2

They are separate things. Every Java web applications based on Servlet technology will have a servlet context, whether it's a spring application or not. In contrast, the ApplicationContext is a Spring thing; in very simple terms, it's a container to hold Spring beans.

To initiate the value for both ApplicationContext and ServletContext, in web.xml, we will add something in context-param tag.

It would help if you quote an example for this, because, as far as I know, context-param is used for ServletContext, and not ApplicationContext.

Update:

You can use a context-param to provide the locations of the root application context configuration files, as below.

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

Solution 3

ApplicationContext is Spring's container.

It's used to wire the configurations from Spring beans together and use them for the application.

Use ApplicationContext if you want to retrieve the information of Spring beans.

Use ServletContext if you want to get/set attribute those shared to all Servlet.

Share:
42,287
Admin
Author by

Admin

Updated on December 23, 2020

Comments

  • Admin
    Admin over 3 years

    I get confused between the two ApplicationContext and ServletContext when it comes to Spring MVC Application. I know that There is just only One ApplicationContext per Spring Web Application and there is also just only One ServletContext per web application. To initiate the value for both ApplicationContext and ServletContext, in web.xml, we will add something in context-param tag.

    That is the point that makes me confused. What are the differences between these two (i know ApplicationContext has some methods to work with beans)? and When we would use ApplicationContext and When we would use ServletContext?

  • Admin
    Admin almost 9 years
    Thank you for your answer, Sanjay. I read some documents say that To configure value for root application context in spring, we need to add some values in context-param in web.xml.
  • Sanjay
    Sanjay almost 9 years
    Maybe they are saying that you can just provide the locations of root application context configuration files by using a context-param. See my updated answer.
  • Pavel Horal
    Pavel Horal over 5 years
  • Madhu
    Madhu over 5 years
    I do see the OP is in the 'context' of Spring framework, but the title itself is more general. ApplicationContext.getContext(java.lang.String uri) link returns an object of ServletContext. ApplicationContext is not just a Spring's construct.