The Refresh method of the spring container

Source: Internet
Author: User
Tags event listener prepare reset

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.