Spring's applicationcontext contains all the features of the beanfactory, as well as additional features.
The refresh () method of the Classpathxmlapplicationcontext parent class Abstractapplicationcontext.
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) {if (logger.iswarnenabled ()) {Logger.warn ("Exception encountered
During context initialization-"+" Cancelling refresh attempt: "+ ex);
}//Destroy already created singletons to avoid dangling resources.
Destroybeans ();
Reset ' active ' flag.
CancelRefresh (ex);
Propagate exception to caller.
Throw ex; } finally {//Reset common introspection caches in Spring's core, since we//might not ever nee D Metadata for singleton beans anymore ... resetcommoncaches (); }
}
}
1.prepareBeanFactory method, register a beanpostprocessor.
Beanfactory.addbeanpostprocessor (New Applicationcontextawareprocessor (this));
The function of this class is to add various aware functions, such as Applicationcontextaware.
2.invokeBeanFactoryPostProcessors method, call Beanfactorypostprocessors.
This method calls all the Beanfactorypostprocessor again.
Postprocessorregistrationdelegate.invokebeanfactorypostprocessors (Beanfactory, GetBeanFactoryPostProcessors ());
2.1 If Beanfactory is a beandefinitionregistry type
Traverse the Beanfactorypostprocessors list, and if it is a beandefinitionregistrypostprocessor type, strongly turn the custom method on the call.
Beandefinitionregistrypostprocessor (hard-coded is called This.beanFactoryPostProcessors.add (postprocessor) to register the pre-encoded Method in the Beanfactorypostprocessors)
Put it in registrypostprocessors.
Beanfactorypostprocessor hard-coded registrations into regularpostprocessors
is Beandefinitionregistrypostprocessor or beanfactorypostprocessor, if the former call the first class method, in all calls to the parent class postprocessbeanfactory.
2.2 If Beanfactory is a beandefinitionregistry type
The Postprocessbeanfactory method is called directly.
3 registerbeanpostprocessors
This method registers beanpostprocessors, which is only called when you are Getbean.
Registerlisteners ()
A hard-coded event listener (in Applicationlisteners), and an XML-configured event listener, And the events in the earlyapplicationevents are placed into the event broadcaster Applicationeventmulticaster.
The earlyapplicationevents effect is that when you call the Publishevent method, the event broadcaster is not initialized before it is put into publishevent.
。