Debug Petstore-web Add the source code package that the project relies on in the Debug Configurations Panel's Source tab page spring-web-3.2.7.release-sources.jar& Spring-context-3.2.10.release-sources.jar&citrus-webx-all-3.2.2-sources.jar
Initial configuration:
Introducing WEBX3 requires the following listener to be introduced in the Web. xml file
Log system initialization
<listener><listener-class>com.alibaba.citrus.logconfig.logconfiguratorlistener</listener-class ></listener>
Webx3 Framework Initialization Loading /web-inf/webx.xml,/web-inf/webx-*.xml
<listener><listener-class>com.alibaba.citrus.webx.context.webxcontextloaderlistener</ Listener-class></listener>
Request Log Context Settings
<filter-class>com.alibaba.citrus.webx.servlet.setloggingcontextfilter</filter-class></filter >
Request interception
<filter-class>com.alibaba.citrus.webx.servlet.WebxFrameworkFilter</filter-class></filter>
entrance Webxcontextloaderlistener :
Webxcontextloaderlistener inherits from Spring's Contextloaderlistener. The starting entry method is the createcontextloader of the parent class, which is to create and initialize the Contextloader object as the context loader for the entire application, and Webxcontextloaderlistener does not replicate this method of the parent class. Instead, the Createcontextloader method is replicated, and the Webxcomponentsloader is set to Contextloader.
Webxcomponentsloader :
Webxcomponentsloader inherits from Contextloader. Covers several of these methods, such as: Initwebapplicationcontext,determinecontextclass,customizecontext,tostring method. The more important one is Determinecontextclass and Customizecontext.
Determinecontextclass
Determinecontextclass This method is to define the Webapplicationcontext implementation class as Webxcomponentscontext. The default processing for parent class Contextloader is to read the following configuration information from the contextloader.properties in the directory where it resides. Webxcomponentsloader handles the configuration file by Webxcomponentscontext this class.
Customizecontext
The main method of Customizecontext is to set Webxcomponentscontext loader to Webxcomponentsloader, This invokes the Webxcomponentscontext Finishrefresh method when executing the Finishrefresh method, and the method calls the Postprocessbeanfactory method of the loader.
The Postprocessbeanfactory method of Webxcomponentsloader is called at the beginning of the creation of beanfactory, and it creates a bean in beanfactory. The corresponding class is Webxcomponentscreator, this class inherits from Beanfactorypostprocessor, and its order is set at the bottom, So it executes after the Propertyplaceholderconfigurer executes.
Createwebapplicationcontext
In the Createwebapplicationcontext method of Contextloader, Determinecontextclass is called the corresponding method in Webxcomponentsloader, Set the ApplicationContext to Webxcomponentscontext.
Webxcomponentscontext :
The Refresh method of Webxcomponentscontext is defined in the Org.springframework.context.support.AbstractApplicationContext.refresh (), The code is as follows
Public void Refresh () throws Beansexception, illegalstateexception { synchronized (this. startupshutdownmonitor) { Prepare this context for refreshing. Preparerefresh (); Tell the subclass to refresh the internal bean factory. Configurablelistablebeanfactory beanfactory = Obtainfreshbeanfactory (); Prepare The Bean factory for use with this context. Preparebeanfactory (beanfactory); Try { Allows post-processing of the Bean factory in context subclasses. Postprocessbeanfactory (beanfactory); Invoke factory processors registered as beans in the context. Invokebeanfactorypostprocessors (beanfactory); Register Bean processors that intercept bean creation. Registerbeanpostprocessors (beanfactory); Initialize message source for this context. Initmessagesource (); Initialize Event Multicaster for this context. Initapplicationeventmulticaster (); Initialize other special beans in specific context subclasses. Onrefresh (); Check for listener beans and register them. Registerlisteners (); Instantiate all remaining (Non-lazy-init) singletons. Finishbeanfactoryinitialization (beanfactory); Last Step:publish corresponding event. Finishrefresh (); } Catch (Beansexception ex) { Destroy already created singletons to avoid dangling resources. Beanfactory.destroysingletons (); Reset ' active ' flag. CancelRefresh (ex); Propagate exception to caller. throw ex; } } } |
(1) Obtainfreshbeanfactory method. The Refreshbeanfactory method code is as follows, the Createbeanfactory method is covered in the xmlwebapplicationcontext of WEBX3, An object of type Inheritablelistablebeanfactory is created as a bean factory, which is a bean factory that WEBX3 extends to spring.
protected Final void refreshbeanfactory () throws beansexception { if (Hasbeanfactory ()) { Destroybeans (); Closebeanfactory (); } Try { Defaultlistablebeanfactory beanfactory = Createbeanfactory (); Customizebeanfactory (beanfactory); Loadbeandefinitions (beanfactory); synchronized (this. beanfactorymonitor) { this. beanfactory = Beanfactory; } }catch (IOException ex) { Throw New Applicationcontextexception ("I/O error parsing XML document for application context [" + getdisplayname () + "]", ex); } } |
Inheritablelistablebeanfactory from this class of comments can be seen in its two important functions:
1) The child context inherits the parent and can share the configuration and resources in the parental context;
2) Child context cannot overwrite an existing Resolvabledependencies object in the parent context. Otherwise, Webapplicationcontext will automatically register non-singleton request objects so that the child context cannot get the singleton proxy registered in the parent context. With this class, you can inject objects such as request into the module, which is actually injected into the module as a proxy, and the actual content of the object is to get the actual object from the current thread and populate it at run time.
Customizebeanfactory Complete Annotation registration. The Allowbeandefinitionoverriding,allowcircularreferences property is set to control whether beans in bean fanctory can be overridden and circular referenced.
The Loadbeandefinitions method is used to load the beans in the application. Method defines the Xmlbeandefinitionreader and sets how to parse the XML document, and then calls the Initbeandefinitionreader method, which invokes the The Xmlwebapplicationcontext class of Springext, by Initbeandefinitionreader This method, handles the corresponding configuration extension points. Finally call Loadbeandefinitions parse XML document, by reading/web-inf/webx-*.xml and/web-inf/ Webx.xml, learns the bean to be loaded, and then calls Registerbeandefinition in Abstractbeandefinitionparser's Parse method to register the bean information with the beanfactory beandefinition Map, but did not complete the initialization of the operation, initialization of this step in the subsequent.
(2) The Postprocessbeanfactory method is an important step in the initialization process. The method will eventually call Webxcomponentsloader's Postprocessbeanfactory method, manually registering the Webxcomponentscreator class to beanfactory to guarantee the order of initialization. In Webx3, since the initialization of components relies on webxconfiguration, and the creation of Webxconfiguration instances uses propertyplaceholderconfigure, So these objects are created in a certain order. First initialize the propertyplaceholderconfigure, then Webxcomponentscreator, and finally the other beanfactorypostprocessors.
public void postprocessbeanfactory ( Configurablelistablebeanfactory beanfactory) { beandefinitionbuilder builder = Beandefinitionbuilder. genericbeandefinition (Webxcomponentscreator. class ); builder.addconstructorargvalue ( this ); beandefinition componentscreator = builder.getbeandefinition (); componentscreator.setautowirecandidate ( false ); beandefinitionregistry registry = (beandefinitionregistry) beanfactory; string name = Springextutil. Generatebeanname (Webxcomponentscreator. class . GetName (), registry); registry.registerbeandefinition (name, componentscreator); } |
(3) The Invokebeanfactorypostprocessors method is divided into 3 steps.
The first step is to execute the class that implements the Beanfactorypostprocessor interface. These classes are added to the ApplicationContext by the Addbeanfactorypostprocessor method (Propertyplaceholderconfigurer);
The second step is to implement the bean (Webxcomponentscreator) that implements the Beanfactorypostprocessor and priorityordered interfaces according to the priority.
The third step is to implement the bean (Requestcontextbeanfactorypostprocessor) that implements the Beanfactorypostprocessor and ordered interfaces according to the priority.
Finally, execute the bean that implements only the Beanfactorypostprocessor interface.
(4) Registerbeanpostprocessors method. Registers a class that implements the Beanpostprocessor interface, similar in order to Beanfactorypostprocessor.
(5) Finishbeanfactoryinitialization method. The Getbean method is used to implement the initialization of the bean.
(6) Finishrefresh creates additional sub-component based on the components created earlier.
Public void Finishrefresh () { Components.getwebxrootcontroller (). Onfinishedprocesscontext (); for (Webxcomponent component:components) { Loginbothservletandloggingsystem ("Initializing Spring sub Webapplicationcontext:" + component.getname ()); Webxcomponentcontext WCC = (webxcomponentcontext) component.getapplicationcontext (); Webxcontroller controller = Component.getwebxcontroller (); Wcc.refresh (); Controller.onfinishedprocesscontext (); } Loginbothservletandloggingsystem ("Webxcomponents:initialization completed"); } |
Start complete.
The class diagram is as follows:
WEBX3 START Process