Spring integrate Web add Spring JAR package to Web project. Jar spring-context-4.0.0.release.jar Spring-core-4.0.0.release.jar Spring-beans-4.0.0.release Jar Spring-aspects -4.0.0.release.jar Spring Aspects -4.0.0.release.jar Spring Aspects -4.0.0.release.jar Com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar JDBC – the ORM package Jar spring-orm-4.0.0.release. jar Spring-tx-4.0.0.release. jar Spring Web integration package Spring-web-4.0.0.release.jar test package spring-test-4.0.0.release.jar

Integrating Spring and the Web container consists of two steps: Import spring-web-4.0.0.release.jar 2, on the web. Org that is configured in the XML configuration file. The springframework. Web. Context. The ContextLoaderListener listener listens ServletContext initialization 3. Configure the contextConfigLocation context parameter in the web. XML configuration file. Configure the location of the Spring configuration file to be used to initialize the Spring container

Configure it in web.xml

<! -- needed for ContextLoaderListener --> <! <context-param> <param-name>contextConfigLocation</param-name> <param-value>location</param-value> </context-param> <! -- Bootstraps the root web application context before servlet initialization --> <! This listener creates the Spring IOC container when the Web project starts. --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>Copy the code

The listener reads the Spring configuration file and initializes the Spring IOC container when the Web project starts. The Spring IOC container object is stored in the ServletContext domain object and we can use the Spring IOC container object. We can dig into the underlying source code: public class ContextLoaderListener extends ContextLoader implements ServletContextListener You can see that it implements a ServletContextListener, The ServletContextListener listens for the creation and destruction of the servletContext object

// Method descriptor #5 (Ljavax/servlet/ServletContextEvent;) V public abstract void contextInitialized(javax.servlet.ServletContextEvent arg0); // Method descriptor #5 (Ljavax/servlet/ServletContextEvent;) V public abstract void contextDestroyed(javax.servlet.ServletContextEvent arg0);Copy the code

ContextInitialized creates the servletContext. ContextDestroyed the servletContext

The creation of the servletContext object is created when the Web project starts, Need to have a web project start-up time springIOC container can have a look at our code before @ ContextConfiguration (locations = “classpath: applicationContext. XML”) or ApplicationContext ApplicationContext = new ClassPathXmlApplicationContext (” the applicationcontext.xml “); Both create Spring’s IOC container objects

When we create Spring’s IOC container object we need the Spring configuration file: applicationContext.xml which we configure in web.xml

<! <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>Copy the code

The following listener reads the above argument as soon as it starts: configuration file path

<! This listener creates the Spring IOC container when the Web project starts. --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>Copy the code

We can use the DeBug the source ContextLoaderListener initWebApplicationContext (event. GetServletContext ()); Find ContextLoader initWebApplicationContext methods

CreateWebApplicationContext create a Spring container object, you can see it has value

Can continue to look down configureAndRefreshWebApplicationContext configuration and refresh the Spring container

We go inside its method body and find the following code

String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);

It reads the value of <param-name>contextConfigLocation</param-name> in web.xml to get the initialization parameter, Get our own value > < param – write the classpath: the applicationcontext.xml value > < param –

Put it in the Spring container

Perform to wac. The refresh (); After executing this code, our configuration file applicationContext.xml has all been loaded and the bean object has been created.

Back in execution to the initWebApplicationContext method servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

The enclosing context



It puts the container object into the servletContext domain object, which is used in web projects to get the Spring container object from the servletContext object

This is the underlying principle of configuration in web.xml

How do we get the Spring container object, WebApplicationContext?

You can obtain the WebApplicationContext object as follows: Method 1 (not recommended) : getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); This one is easy to understand, we put the container object in the servletContext field object, now we can get it, but the long list is not easy to remember, so it is not recommended

Method two (recommended) : WebApplicationContextUtils. GetWebApplicationContext (getServletContext ()) using this is better, not our memories, and its underlying and a method is the same, Also get getAttribute