Ioc容器BeanPostProcessor-Spring 源碼(3),springiocbean
Ioc容器BeanPostProcessor-Spring 源碼(3)
目錄:
Ioc容器beanDefinition-Spring 源碼(1)
Ioc容器依賴注入-Spring 源碼(2)
Ioc容器BeanPostProcessor-Spring 源碼(3)
如果這個介面的某個實作類別被註冊到某個容器,那麼該容器的每個受管Bean在調用初始化方法之前,都會獲得該介面實作類別的一個回調。容器調用介面定義的方法時會將該受管Bean的執行個體和名字通過參數傳入方法,進過處理後通過方法的傳回值返回給容器。
根據這個原理,我們就可以很輕鬆的自訂受管Bean。 以下為使用例子的代碼:
public class BeanPostProcessorTest implements BeanPostProcessor, Ordered { public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { return o; } public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("beanName:"+s); return o; } /** * 多個BeanPostProcessor 用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" />
在 《定時任務管理中心》文中就是用這種方式講需要管理的bean過濾出來的。
這篇就解析一下spring 源碼是哪裡觸發的,如何完成的。
postProcessAfterInitialization的調用鏈
在上一篇中提到,doCreateBean就是建立bean的入口,在做線方法:populateBean(beanName, mbd, instanceWrapper);之後執行的initializeBean(beanName, exposedObject, mbd);觸發的,直接看initializeBean代碼:
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()) { // 執行ProcessorsBefore的方法 wrappedBean = applyBeanPostProcessorsBeforeInitialization(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()) { //執行ProcessorsAfter的方法 wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean;}
這裡就先只看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;}
好了,到這裡就調用到了所有BeanPostProcessors的實現。前面的代碼其實有點像觀察者模式的調用。
那麼根據order順序來執行BeanPostProcessors實現就是按順序在放這個List<BeanPostProcessor>裡,實現的代碼如下:
public static void registerBeanPostProcessors( ConfigurableListableBeanFactory 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. // 實現Ordered介面的 List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>(); // 未實現Ordered介面的 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) { //反而沒有實現ordered的先放 internalPostProcessors.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));}
----------------------
永遠愛汀汀