Spring createBean过程中BeanPostProcessor的处理机会
2016-11-24 21:51
447 查看
Spring IOC在实例化bean的过程中,BeanPostProcessor有机会在bean Instantiation前后、initialize前后进行处理,下面了解下分别做了什么。
其中,红色步骤为BeanPostProcessor的相关处理。
AbstractAutowireCapableBeanFactory.java
AbstractAutowireCapableBeanFactory.java
BeanPostProcessor处理时序图
这里只涉及BeanPostProcessor处理,其他过程忽略:其中,红色步骤为BeanPostProcessor的相关处理。
相关源码分析
AbstractAutowireCapableBeanFactory.java// 实例化前的BeanPostProcessor处理,这里有机会返回bean的代理对象 protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) { Object bean = null; if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { // Make sure bean class is actually resolved at this point. if (mbd.hasBeanClass() && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { bean = applyBeanPostProcessorsBeforeInstantiation(mbd.getBeanClass(), beanName); if (bean != null) { bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } } mbd.beforeInstantiationResolved = (bean != null); } return bean; } protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName); if (result != null) { return result; } } } return null; } 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; }
AbstractAutowireCapableBeanFactory.java
// BeanPostProcessor有机会修改RootBeanDefinition mbd protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) throws BeansException { try { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof MergedBeanDefinitionPostProcessor) { MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); } } } catch (Exception ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Post-processing failed of bean type [" + beanType + "] failed", ex); } }
AbstractAutowireCapableBeanFactory.java
// BeanPostProcessor在bean初始化前的处理 public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result = beanProcessor.postProcessBeforeInitialization(result, beanName); if (result == null) { return result; } } return result; } // bean的初始化 protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean); if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { if (logger.isDebugEnabled()) { logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); } if (System.getSecurityManager() != null) { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { ((InitializingBean) bean).afterPropertiesSet(); return null; } }, getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { ((InitializingBean) bean).afterPropertiesSet(); // bean的初始化 } } if (mbd != null) { String initMethodName = mbd.getInitMethodName(); if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { invokeCustomInitMethod(beanName, bean, mbd); } } } // BeanPostProcessor在bean初始化后的处理 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; }
相关文章推荐
- 关于spring中处理相同id的bean的问题
- Spring 学习之 bean的前、后处理
- spring 装配bean后执行后台进程任务处理线程的处理方式
- Spring3.2 Http 请求处理过程笔记
- Spring3.2 Http 请求处理过程笔记
- Spring bean配置中属性值由String到实际类型的动态转化过程及PropertyEditor类的应用
- spring——bean创建过程
- 使用spring的特殊bean --- 对bean进行后处理和对bean工厂进行后处理简单了解
- spring 后置处理器BeanFactoryPostProcessor和BeanPostProcessor的用法和区别
- 浅谈spring——bean创建过程(四)
- 有关spring中bean的@Scope 和richfaces 中表单分页的处理方式
- Spring处理id相同的bean
- spring中bean初始化过程
- Spring BeanFactory、ApplicationContext层次结构和BeanDefinition解析过程
- spring中基于BeanPostProcessor动态替换指定的bean
- Spring的IoC学习笔记之BeanFactoryPostProcessor
- spring处理bean
- Spring中Bean的初始化过程
- Spring创建Bean的过程 【摘抄】
- Spring笔记:定制Bean的初始化和销毁过程