IOC container (3)-beanpostprocessor-spring Source code

Source: Internet
Author: User

IOC container beanpostprocessor-spring source code (3)

Directory:

IOC container beandefinition-spring source code (1)

IOC container dependency injection-spring source (2)

IOC container beanpostprocessor-spring source code (3)

If an implementation class of this interface is registered to a container, each managed bean of that container obtains a callback for that interface implementation class before invoking the initialization method. When the container invokes the method defined by the interface, the instance and name of the managed bean are passed through the parameter to the method, which is returned to the container by the return value of the Method.

Based on this principle, we can easily customize the managed Bean. Here is the code that uses the Example:
 public classBeanpostprocessortestImplementsbeanpostprocessor, Ordered { publicObject Postprocessbeforeinitialization (object o, String S)throwsbeansexception {returno; }     publicObject Postprocessafterinitialization (object o, String S)throwsbeansexception {System.out.println ("beanname:" +s); returno; }    /*** Multiple beanpostprocessor are sorted by order *@return     */     public intGetOrder () {return0; }} public Static voidMain (string[] Args)throwsException {applicationcontext AppContext=NewClasspathxmlapplicationcontext ("spring-customer.xml"); }
<class= "aspect.test.spring.BeanPostProcessorTest"/>

In the "timed task management center", This is the way to tell the bean that needs to be managed to filter Out.

This article will explain where the spring source code is triggered, how to Complete.

Postprocessafterinitialization's Call Chain

As mentioned in the previous article, Docreatebean is the entry for the creation of the bean, in the line method: Populatebean (beanname, mbd, instancewrapper), followed by Initializebean (beanname, exposedobject, mbd); trigger, See Initializebean code directly:

protectedObject Initializebean (FinalString beanname,FinalObject bean, rootbeandefinition mbd) {   if(system.getsecuritymanager ()! =NULL) {accesscontroller.doprivileged (NewPrivilegedaction<object>() {@Override publicObject Run () {invokeawaremethods (beanname, bean); return NULL;   }}, Getaccesscontrolcontext ()); }   Else{invokeawaremethods (beanname, bean); } Object Wrappedbean=bean; if(MBD = =NULL|| !mbd.issynthetic ()) {      //methods of executing ProcessorsbeforeWrappedbean =applybeanpostprocessorsbeforeinitialization (wrappedbean, beanname); }   Try{invokeinitmethods (beanname, wrappedbean, mbd); }   Catch(throwable Ex) {Throw Newbeancreationexception ((MBD!=NULL? Mbd.getresourcedescription ():NULL), beanname,"invocation of Init method failed", ex); }   if(MBD = =NULL|| !mbd.issynthetic ()) {     //methods of executing ProcessorsafterWrappedbean =applybeanpostprocessorsafterinitialization (wrappedbean, beanname); }   returnwrappedbean;}

Here is the first look at applybeanpostprocessorsafterinitialization:

 public   Object Applybeanpostprocessorsafterinitialization (Object existingbean, String beanname)  throws   beansexception {Object result  = for   (beanpostprocessor beanprocessor: Getbeanpostprocessors ()) {result  = if  (result = = null   return   result; }}  return   result;}  

All right, Here we go. all beanpostprocessors implementations are Called. The preceding code is actually a bit like the invocation of the Observer Pattern.

Then the execution of the beanpostprocessors implementation according to order sequence is in order to put this list<beanpostprocessor>, the implementation of the code is as Follows:

 public Static voidregisterbeanpostprocessors (configurablelistablebeanfactory beanfactory, abstractapplicationcontext Applicationcontext) {string[] postprocessornames= Beanfactory.getbeannamesfortype (beanpostprocessor.class,true,false); //Register beanpostprocessorchecker that logs a info message when//A bean is created during beanpostprocessor instantiation and i.e. when//a bean is not eligible for getting processed by all Beanpostprocessors.   intBeanprocessortargetcount = Beanfactory.getbeanpostprocessorcount () + 1 +postprocessornames.length; Beanfactory.addbeanpostprocessor (NewBeanpostprocessorchecker (beanfactory, beanprocessortargetcount)); //separate between beanpostprocessors that implement priorityordered,//Ordered, and the Rest. //implementation of the ordered interfaceList<beanpostprocessor> priorityorderedpostprocessors =NewArraylist<beanpostprocessor>(); //the ordered interface is not implementedList<beanpostprocessor> internalpostprocessors =NewArraylist<beanpostprocessor>(); List<String> Orderedpostprocessornames =NewArraylist<string>(); List<String> Nonorderedpostprocessornames =NewArraylist<string>();  for(String ppname:postprocessornames) {if(beanfactory.istypematch (ppname, Priorityordered.class) ) {beanpostprocessor pp= Beanfactory.getbean (ppname, Beanpostprocessor.class);         Priorityorderedpostprocessors.add (pp); if(ppinstanceofMergedbeandefinitionpostprocessor) {            //instead of realizing Ordered's first releaseInternalpostprocessors.add (pp); }      }      Else if(beanfactory.istypematch (ppname, Ordered.class) {orderedpostprocessornames.add (ppname); }      Else{nonorderedpostprocessornames.add (ppname); }   }   //first , register the beanpostprocessors that implement Priorityordered.sortpostprocessors (beanfactory, priorityorderedpostprocessors);   Registerbeanpostprocessors (beanfactory, priorityorderedpostprocessors); //Next, Register the beanpostprocessors that implement Ordered.List<beanpostprocessor> orderedpostprocessors =NewArraylist<beanpostprocessor>();  for(String ppname:orderedpostprocessornames) {beanpostprocessor pp= Beanfactory.getbean (ppname, Beanpostprocessor.class);      Orderedpostprocessors.add (pp); if(ppinstanceofMergedbeandefinitionpostprocessor)      {internalpostprocessors.add (pp);   }} sortpostprocessors (beanfactory, orderedpostprocessors);   Registerbeanpostprocessors (beanfactory, orderedpostprocessors); //now , register all regular Beanpostprocessors.List<beanpostprocessor> nonorderedpostprocessors =NewArraylist<beanpostprocessor>();  for(String ppname:nonorderedpostprocessornames) {beanpostprocessor pp= Beanfactory.getbean (ppname, Beanpostprocessor.class);      Nonorderedpostprocessors.add (pp); if(ppinstanceofMergedbeandefinitionpostprocessor)      {internalpostprocessors.add (pp);   }} registerbeanpostprocessors (beanfactory, nonorderedpostprocessors); //Finally, Re-register all internal Beanpostprocessors.sortpostprocessors (beanfactory, internalpostprocessors);     Registerbeanpostprocessors (beanfactory, internalpostprocessors); Beanfactory.addbeanpostprocessor (NewApplicationlistenerdetector (applicationcontext));}

----------------------

Always in love with The Ting

IOC container (3)-beanpostprocessor-spring Source code

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.