What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

前端 未结 5 843
迷失自我
迷失自我 2020-11-22 06:53

What is the difference between Application Context and Web Application Context?

I am aware that WebApplicationContext is used for Spring MVC architectur

5条回答
  •  春和景丽
    2020-11-22 07:26

    Web Application context extended Application Context which is designed to work with the standard javax.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 are 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 application context. In web apps as default there are two hierarchy levels, root and servlet contexts: Servlet and root context.

    This 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 servlet 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.

    See also: Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

提交回复
热议问题