"Spring-source analysis" notes
1, custom BeanPostProcessor
@Component public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean); return bean; } }
2, wherein the function call chain postProcessBeforeInitialization follows:
When the vessel started: refresh () -> finishBeanFactoryInitialization (beanFactory) -> beanFactory.preInstantiateSingletons () -> this.getBean (beanName) ->
this.doGetBean(name, (Class)null, (Object[])null, false)--this.getSingleton(beanName, new ObjectFactory<Object>()-->
singletonFactory.getObject()-->AbstractBeanFactory.this.createBean(beanName, mbd, args)-->this.doCreateBean(beanName, mbdToUse, args)
-->this.initializeBean(beanName, exposedObject, mbd)
Before performing the initializeBean (), it is executed first populateBean () function, which is mainly assigned to the various attributes of Bean.
this.populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { exposedObject = this.initializeBean(beanName, exposedObject, mbd); }
3, in initializeBean (beanName, exposedObject, mbd) observed calling code
if (mbd == null || !mbd.isSynthetic()) { wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName); } //初始化方法执行代码 try { this.invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable var6) { throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6); } if (mbd == null || !mbd.isSynthetic()) { wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); }
4. Check the function code applyBeanPostProcessorsBeforeInitialization and applyBeanPostProcessorsAfterInitialization
Traversing all the BeanPostProcessor obtained container; executed one by one beforeInitialization, but returned a null, out of for loop, the latter does not execute BeanPostProcessor.postProcessorsBeforeInitialization
For each container Bean instance, step 3 if they meet the conditions of the statement, will perform these two functions, the interface will enter BeanPostProcessor not realize these two functions.
@Override public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { result = processor.postProcessBeforeInitialization(result, beanName); if (result == null) { return result; } } return result; } @Override public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { result = processor.postProcessAfterInitialization(result, beanName); if (result == null) { return result; } } return result; }