Introduction to WebApplicationContext

   WebApplicationContext is specially prepared for web applications. It allows loading configuration files from the path relative to the web root directory to complete the initialization work. The reference of ServletContext can be obtained from WebApplicationContext. The entire web application context object will be placed as an attribute in ServletContext. So that the web application can access the spring context, the getWebApplicationContext(ServletContext src) method of WebApplicationContextUtils is provided in spring to obtain the WebApplicationContext object

 

WebApplicationContext extends ApplicationContext. A constant ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE is defined in WebApplicationContext. When the context starts,

The WebApplicationContext is placed in the ServletContext attribute list with this key,

 

public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
        return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    }

ConfigurableWebApplicationContext extends WebApplicationContext, which allows instantiation by configuration, while setting two important methods

  setServletContext(ServletContext context) sets the web application context for spring so that the two can be integrated

setConfigLocations(String[]locations) Set the file address of Spring configuration

 

The initialization of webApplicationContext requires ServletContext, that is to say, it needs the premise of the web container to complete the startup work.  The startup of the web container can be realized by configuring the self-starting servlet or web container monitoring in web.xml 

Spring provides servlets and web container listeners that start the WebApplicationContext respectively

  org.springframework.web.context.ContextLoaderListener  

 org.springframework.web.context.ContexLoaderServlet This method is currently deprecated

copy code
!--Load Spring configuration files from the classpath, classpath specifically refers to loading from the classpath-->
     < context-param > 
        < param-name > contextConfigLocation </ param-name > 
        < param-value >
            classpath:smart-context.xml
        </ param-value > 
    </ context-param > 
    <!-- The listener responsible for starting the spring container can also declare a self-starting Servlet ContextLoaderServlet --> 
    < listener > 
        < listener-class > org.springframework.web.context. ContextLoaderListener </ listener-class > 
    </ listener >
copy code

 

If you use the java class of @Configuration to provide configuration information, the configuration web.xml configuration is modified as follows

copy code
 <!-- By specifying the context parameter, let Spring use AnnotationConfigWebApplicationContext to start the container instead of XmlWebApplicationContext. By default, XmlWebApplicationContext is used when it is not configured --> 
    < context-param > 
        < param-name > contextClass </ param-name > 
        < param-value >
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </ param-value > 
    </ context-param > 
<!-- Specify the class marked with @Configuration, multiple can be separated by commas --> 
    < context-param > 
        < param-name > contextConfigLocation </ param-name > 
        < param-value > com.example.Car,com.example.Boss </ param-value > 
    </ context-param > 
    <!-- listener will use AnnotationConfigWebApplicationContext according to above configuration
    According to contextConfigLocation
    The specified configuration class starts the Spring container --> 
    < listener > 
        < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > 
    </ listener >

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326116532&siteId=291194637