Ioc container BeanPostProcessor-Spring source code (3), springiocbean

Source: Internet
Author: User

Ioc container BeanPostProcessor-Spring source code (3), springiocbean
Ioc container BeanPostProcessor-Spring source code (3)

 

Directory:

Ioc container beanDefinition-Spring source code (1)

Ioc container dependency injection-Spring source code (2)

Ioc container BeanPostProcessor-Spring source code (3)

 

If an implementation class of this interface is registered to a container, each managed Bean of the container receives a callback of this interface implementation class before calling the initialization method. When the container calls the method defined by the interface, the instance and name of the managed Bean are passed in to the method through parameters. After processing, the return value of the method is returned to the container.

Based on this principle, we can easily customize managed beans. The following code uses an example:
Public class BeanPostProcessorTest implements BeanPostProcessor, Ordered {public Object postProcessBeforeInitialization (Object o, String s) throws BeansException {return o;} public Object terminate (Object o, String s) throws BeansException {System. out. println ("beanName:" + s); return o;}/*** multiple BeanPostProcessor are sorted by order * @ return */public int getOrder () {return 0 ;}} public static void main (String [] args) throws Exception {ApplicationContext appContext = new ClassPathXmlApplicationContext ("Spring-Customer.xml ");}
<bean class="aspect.test.spring.BeanPostProcessorTest" />

In the timer task management center, bean filtering needs to be managed in this way.

This article will explain how spring source code is triggered and completed.

Call chain of postProcessAfterInitialization

 

As mentioned in the previous article, doCreateBean is the entry for bean creation. It is executed in the following line method: populateBean (beanName, mbd, instanceWrapper); initializeBean (beanName, exposedObject, mbd ); the initializeBean code is as follows:

Protected Object initializeBean (final String beanName, final Object bean, RootBeanDefinition mbd) {if (System. getSecurityManager ()! = Null) {AccessController. doPrivileged (new PrivilegedAction <Object> () {@ Override public Object run () {invokeAwareMethods (beanName, bean); return null ;}, getAccessControlContext ());} else {invokeAwareMethods (beanName, bean);} Object wrappedBean = bean; if (mbd = null |! Mbd. isSynthetic () {// Method for executing ProcessorsBefore wrappedBean = invoke (wrappedBean, beanName);} try {invokeInitMethods (beanName, wrappedBean, mbd);} catch (Throwable ex) {throw new BeanCreationException (mbd! = Null? Mbd. getResourceDescription (): null), beanName, "Invocation of init method failed", ex);} if (mbd = null |! Mbd. isSynthetic () {// Method for executing ProcessorsAfter wrappedBean = applyBeanPostProcessorsAfterInitialization (wrappedBean, beanName);} return wrappedBean ;}

Here we will only look at applyBeanPostProcessorsAfterInitialization:

public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)      throws BeansException {   Object result = existingBean;   for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {      result = beanProcessor.postProcessAfterInitialization(result, beanName);      if (result == null) {         return result;      }   }   return result;}

Now, all BeanPostProcessors are called. The previous code is actually a bit like an observer mode call.

The BeanPostProcessors implementation is executed in order according to the order in the List <BeanPostProcessor>. The implementation code is as follows:

Public static void registerBeanPostProcessors (abstrablelistablebeanfactory beanFactory, AbstractApplicationContext applicationContext) {String [] postProcessorNames = beanFactory. getBeanNamesForType (BeanPostProcessor. class, true, false); // Register BeanPostProcessorChecker that logs an info message when // a bean is created during BeanPostProcessor instantiation, I. e. when // a bean is not eligible for getting processed by all BeanPostProcessors. int beanProcessorTargetCount = beanFactory. getBeanPostProcessorCount () + 1 + postProcessorNames. length; beanFactory. addBeanPostProcessor (new BeanPostProcessorChecker (beanFactory, beanProcessorTargetCount); // Separate between BeanPostProcessors that implement PriorityOrdered, // Ordered, and the rest. // List <BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList <BeanPostProcessor> (); // List <BeanPostProcessor> internalPostProcessors = new ArrayList <BeanPostProcessor> (); List <String> orderedPostProcessorNames = new ArrayList <String> (); list <String> nonOrderedPostProcessorNames = new ArrayList <String> (); for (String ppName: postProcessorNames) {if (beanFactory. isTypeMatch (ppName, PriorityOrdered. class) {BeanPostProcessor pp = beanFactory. getBean (ppName, BeanPostProcessor. class); priorityOrderedPostProcessors. add (pp); if (pp instanceof MergedBeanDefinitionPostProcessor) {// instead, internalPostProcessors is not implemented for ordered. 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 = new ArrayList <BeanPostProcessor> (); for (String ppName: orderedPostProcessorNames) {BeanPostProcessor pp = beanFactory. getBean (ppName, BeanPostProcessor. class); orderedPostProcessors. add (pp); if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors. add (pp) ;}} sortPostProcessors (beanFactory, orderedPostProcessors); registerBeanPostProcessors (beanFactory, orderedPostProcessors); // Now, register all regular BeanPostProcessors. list <BeanPostProcessor> nonOrderedPostProcessors = new ArrayList <BeanPostProcessor> (); for (String ppName: nonOrderedPostProcessorNames) {BeanPostProcessor pp = beanFactory. getBean (ppName, BeanPostProcessor. class); nonOrderedPostProcessors. add (pp); if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors. add (pp) ;}} registerBeanPostProcessors (beanFactory, nonOrderedPostProcessors); // Finally, re-register all internal BeanPostProcessors. sortPostProcessors (beanFactory, internalPostProcessors); registerBeanPostProcessors (beanFactory, internalPostProcessors); beanFactory. addBeanPostProcessor (new ApplicationListenerDetector (applicationContext ));}

 

 

 

 

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

Love ting forever

 

Related Article

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.