步驟 | 執行過程 | 描述 |
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,則不會進行第5、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緩存類中 |