spring的bean建立過程

Spring的bean建立過程
步驟 執行過程 描述
1 ThreadLocal.set bean建立以前將beanName的一些屬性放進ThreadLocal,避免多線程建立bean致使問題,併發建立會拋BeanCurrentlyInCreationException異常
2 InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation bean建立以前的回調,若是該方法返回不爲null則不進行bean的建立過程,完成到第13步
3 MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition bean建立以後的回調,處理bean的合併,如Autowire註釋的處理器處理注入信息
4 InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation bean建立以後的回調,若是該方法返回true,則不會進行第五、6步
5 InstantiationAwareBeanPostProcessor.postProcessPropertyValues 處理BeanDefinition的PropertyValues,爲下一步注入屬性打基礎
6 BeanWrapper.setPropertyValues 注入依賴:根據上一步處理的結果(即PropertyValues),將bean的屬性(字段)賦值
7 BeanNameAware.setBeanName bean實現了BeanNameAware接口則會調用
8 BeanClassLoaderAware.setBeanClassLoader bean實現了BeanClassLoaderAware接口則會調用
9 BeanFactoryAware.setBeanFactory bean實現了BeanFactoryAware接口則會調用
10 BeanPostProcessor.postProcessBeforeInitialization

初始化方法調用以前的回調,這其中有個processor進行接口方法回調,執行EnvironmentAware、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware、ApplicationContextAware對應方法緩存

11 InitializingBean.afterPropertiesSet 調用bean的初始化方法,bean實現了InitializingBean接口則會調用afterPropertiesSet方法,PostConstruct註釋修飾的方法也會被當作初始化方法
12 BeanPostProcessor.postProcessAfterInitialization 初始化方法調用以後的回調
13 FactoryBean.getObject 若是返回的對象是FactoryBean,則進一步調用FactoryBean的getObject方法並最終返回此方法返回的對象,不然直接返回原對象
14 ThreadLocal.remove bean建立完成以後清空ThreadLocal
15 ConcurrentHashMap.put 若是是單例模式的bean的則放入map緩存類中
相關文章
相關標籤/搜索