There is a statement in Web. xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/ web-inf/applicationcontext.xml</param-value> </context-param> <listener> < listener-class>org.springframework.web.context.contextloaderlistener</listener-class > </listener>
The Contextloaderlistener class is the starting point for starting, observing
The implementation of the Contextloaderlistener class is as follows:
Public classContextloaderlistenerextendsContextloaderImplementsServletcontextlistener { PublicContextloaderlistener () {} PublicContextloaderlistener (Webapplicationcontext context) {Super(context); } Public voidcontextinitialized (Servletcontextevent event) { This. Initwebapplicationcontext (Event.getservletcontext ()); } Public voidcontextdestroyed (Servletcontextevent event) { This. Closewebapplicationcontext (Event.getservletcontext ()); Contextcleanuplistener.cleanupattributes (Event.getservletcontext ()); }}
Using the Servletcontextlistener interface to hear the boot event, call the Contextloader.initwebapplicationcontext method to complete the boot
The process is in the Contextloader.initwebapplicationcontext event, as follows:
Try { if( This. Context = =NULL) { This. Context = This. Createwebapplicationcontext (ServletContext); } if( This. ContextinstanceofConfigurablewebapplicationcontext) {Configurablewebapplicationcontext CWAC= (Configurablewebapplicationcontext) This. Context; if(!cwac.isactive ()) { if(cwac.getparent () = =NULL) {ApplicationContext parent= This. Loadparentcontext (ServletContext); Trace out this block is empty, that is, the parent container default empty cwac.setparent (parented); } This. Configureandrefreshwebapplicationcontext (CWAC, ServletContext); }} servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute , This. Context); ClassLoader CCL=Thread.CurrentThread (). Getcontextclassloader (); if(CCL = = Contextloader.class. getClassLoader ()) {CurrentContext= This. Context; } Else if(CCL! =NULL) {currentcontextperthread.put (CCL, This. Context); } if(logger.isdebugenabled ()) {Logger.debug ("Published root webapplicationcontext as ServletContext attribute with name [" + Webapplicationcontext.root_web_ Application_context_attribute + "]"); } if(logger.isinfoenabled ()) {LongElapsedTime = System.currenttimemillis ()-StartTime; Logger.info ("Root Webapplicationcontext:initialization completed in" + ElapsedTime + "MS"); } return This. Context; } Catch(RuntimeException var8) {logger.error ("Context initialization Failed", VAR8); Servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, VAR8); ThrowVar8; } Catch(Error var9) {logger.error ("Context initialization Failed", VAR9); Servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, VAR9); ThrowVar9; }
There are two key methods
1:createwebapplicationcontext
Construct an instance of the Configurablewebapplicationcontext class by reflection, that is, the Webapplicationcontext class instance
2:Configureandrefreshwebapplicationcontext
The source code is as follows:
protected void Configureandrefreshwebapplicationcontext (Configurablewebapplicationcontext WAC, ServletContext SC) {
Wac.setservletcontext (SC); Configlocationparam= Sc.getinitparameter ("Contextconfiglocation"); if(Configlocationparam! =NULL) {wac.setconfiglocation (Configlocationparam); } configurableenvironment env=wac.getenvironment (); if(envinstanceofconfigurablewebenvironment) {(configurablewebenvironment) env. Initpropertysources (SC, (servletconfig)NULL); } This. Customizecontext (SC, WAC); Wac.refresh ();
The Configurablewebapplicationcontext created in the first step, and the environment context ServletContext, are passed in.
Gets the XML configuration file, the IOC's application.xml configuration file, and initializes the container property parameters
Refresh () method with emphasis on Configurablewebapplicationcontext
The implementation is as follows:
Public voidRefresh ()throwsbeansexception, illegalstateexception {Object var1= This. Startupshutdownmonitor; synchronized( This. Startupshutdownmonitor) { This. Preparerefresh (); Configurablelistablebeanfactory beanfactory= This. Obtainfreshbeanfactory (); This. Preparebeanfactory (beanfactory); Try { This. Postprocessbeanfactory (beanfactory); This. Invokebeanfactorypostprocessors (beanfactory); This. Registerbeanpostprocessors (beanfactory); This. Initmessagesource (); This. Initapplicationeventmulticaster (); This. Onrefresh (); This. Registerlisteners (); This. Finishbeanfactoryinitialization (beanfactory); This. Finishrefresh (); } Catch(beansexception var9) {if( This. logger.iswarnenabled ()) { This. Logger.warn ("Exception encountered during context initialization-cancelling refresh attempt:" +VAR9); } This. Destroybeans (); This. CancelRefresh (VAR9); ThrowVar9; } finally { This. Resetcommoncaches (); } } }
"Spring Source" Spring Web startup and shutdown