javascript
Spring容器创建过程
Spring容器的refresh()【創(chuàng)建刷新】;
1、prepareRefresh()刷新前的預(yù)處理;
?? ?1)、initPropertySources()初始化一些屬性設(shè)置;子類自定義個性化的屬性設(shè)置方法;
?? ?2)、getEnvironment().validateRequiredProperties();檢驗屬性的合法等
?? ?3)、earlyApplicationEvents= new LinkedHashSet<ApplicationEvent>();保存容器中的一些早期的事件;
2、obtainFreshBeanFactory();獲取BeanFactory;
?? ?1)、refreshBeanFactory();刷新【創(chuàng)建】BeanFactory;
?? ??? ??? ?創(chuàng)建了一個this.beanFactory = new DefaultListableBeanFactory();
?? ??? ??? ?設(shè)置BeanFactory的id;
?? ?2)、getBeanFactory();返回剛才GenericApplicationContext創(chuàng)建的BeanFactory對象;
?? ?3)、將創(chuàng)建的BeanFactory【DefaultListableBeanFactory】返回;
3、prepareBeanFactory(beanFactory);BeanFactory的預(yù)準(zhǔn)備工作(BeanFactory進行一些設(shè)置);
?? ?1)、設(shè)置BeanFactory的類加載器、支持表達式解析器...
?? ?2)、添加部分BeanPostProcessor【ApplicationContextAwareProcessor】
?? ?3)、設(shè)置忽略的自動裝配的接口EnvironmentAware、EmbeddedValueResolverAware、xxx;
?? ?4)、注冊可以解析的自動裝配;我們能直接在任何組件中自動注入:
?? ??? ??? ?BeanFactory、ResourceLoader、ApplicationEventPublisher、ApplicationContext
?? ?5)、添加BeanPostProcessor【ApplicationListenerDetector】
?? ?6)、添加編譯時的AspectJ;
?? ?7)、給BeanFactory中注冊一些能用的組件;
?? ??? ?environment【ConfigurableEnvironment】、
?? ??? ?systemProperties【Map<String, Object>】、
?? ??? ?systemEnvironment【Map<String, Object>】
4、postProcessBeanFactory(beanFactory);BeanFactory準(zhǔn)備工作完成后進行的后置處理工作;
?? ?1)、子類通過重寫這個方法來在BeanFactory創(chuàng)建并預(yù)準(zhǔn)備完成以后做進一步的設(shè)置
======================以上是BeanFactory的創(chuàng)建及預(yù)準(zhǔn)備工作==================================
5、invokeBeanFactoryPostProcessors(beanFactory);執(zhí)行BeanFactoryPostProcessor的方法;
?? ?BeanFactoryPostProcessor:BeanFactory的后置處理器。在BeanFactory標(biāo)準(zhǔn)初始化之后執(zhí)行的;
?? ?兩個接口:BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor
?? ?1)、執(zhí)行BeanFactoryPostProcessor的方法;
?? ??? ?先執(zhí)行BeanDefinitionRegistryPostProcessor
?? ??? ?1)、獲取所有的BeanDefinitionRegistryPostProcessor;
?? ??? ?2)、先執(zhí)行實現(xiàn)了PriorityOrdered優(yōu)先級接口的BeanDefinitionRegistryPostProcessor、
?? ??? ??? ?postProcessor.postProcessBeanDefinitionRegistry(registry)
?? ??? ?3)、在執(zhí)行實現(xiàn)了Ordered順序接口的BeanDefinitionRegistryPostProcessor;
?? ??? ??? ?postProcessor.postProcessBeanDefinitionRegistry(registry)
?? ??? ?4)、最后執(zhí)行沒有實現(xiàn)任何優(yōu)先級或者是順序接口的BeanDefinitionRegistryPostProcessors;
?? ??? ??? ?postProcessor.postProcessBeanDefinitionRegistry(registry)
?? ??? ??? ?
?? ??? ?
?? ??? ?再執(zhí)行BeanFactoryPostProcessor的方法
?? ??? ?1)、獲取所有的BeanFactoryPostProcessor
?? ??? ?2)、看先執(zhí)行實現(xiàn)了PriorityOrdered優(yōu)先級接口的BeanFactoryPostProcessor、
?? ??? ??? ?postProcessor.postProcessBeanFactory()
?? ??? ?3)、在執(zhí)行實現(xiàn)了Ordered順序接口的BeanFactoryPostProcessor;
?? ??? ??? ?postProcessor.postProcessBeanFactory()
?? ??? ?4)、最后執(zhí)行沒有實現(xiàn)任何優(yōu)先級或者是順序接口的BeanFactoryPostProcessor;
?? ??? ??? ?postProcessor.postProcessBeanFactory()
6、registerBeanPostProcessors(beanFactory);注冊BeanPostProcessor(Bean的后置處理器)【 intercept bean creation】
?? ??? ?不同接口類型的BeanPostProcessor;在Bean創(chuàng)建前后的執(zhí)行時機是不一樣的
?? ??? ?BeanPostProcessor的子接口類型有如下:
?? ??? ?DestructionAwareBeanPostProcessor、
?? ??? ?InstantiationAwareBeanPostProcessor、
?? ??? ?SmartInstantiationAwareBeanPostProcessor、
?? ??? ?MergedBeanDefinitionPostProcessor【實現(xiàn)該接口的后置處理器,在掃描所有后置處理器時會被在到internalPostProcessors中】、
?? ??? ?
?? ??? ?1)、獲取所有的 BeanPostProcessor;后置處理器都默認可以通過PriorityOrdered、Ordered接口來執(zhí)行優(yōu)先級
?? ??? ?2)、先注冊PriorityOrdered優(yōu)先級接口的BeanPostProcessor;
?? ??? ??? ?把每一個BeanPostProcessor;添加到BeanFactory中
?? ??? ??? ?beanFactory.addBeanPostProcessor(postProcessor);
?? ??? ?3)、再注冊O(shè)rdered接口的
?? ??? ?4)、最后注冊沒有實現(xiàn)任何優(yōu)先級接口的
?? ??? ?5)、最終注冊MergedBeanDefinitionPostProcessor;
?? ??? ?6)、注冊一個ApplicationListenerDetector;來在Bean創(chuàng)建完成后檢查是否是ApplicationListener,如果是
?? ??? ??? ?applicationContext.addApplicationListener((ApplicationListener<?>) bean);
7、initMessageSource();初始化MessageSource組件(做國際化功能;消息綁定,消息解析);
?? ??? ?1)、獲取BeanFactory
?? ??? ?2)、看容器中是否有id為messageSource的,類型是MessageSource的組件
?? ??? ??? ?如果有賦值給messageSource,如果沒有自己創(chuàng)建一個DelegatingMessageSource;
?? ??? ??? ??? ?MessageSource:取出國際化配置文件中的某個key的值;能按照區(qū)域信息獲取;
?? ??? ?3)、把創(chuàng)建好的MessageSource注冊在容器中,以后獲取國際化配置文件的值的時候,可以自動注入MessageSource;
?? ??? ??? ?beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);?? ?
?? ??? ??? ?MessageSource.getMessage(String code, Object[] args, String defaultMessage, Locale locale);
8、initApplicationEventMulticaster();初始化事件派發(fā)器;
?? ??? ?1)、獲取BeanFactory
?? ??? ?2)、從BeanFactory中獲取applicationEventMulticaster的ApplicationEventMulticaster;
?? ??? ?3)、如果上一步?jīng)]有配置;創(chuàng)建一個SimpleApplicationEventMulticaster
?? ??? ?4)、將創(chuàng)建的ApplicationEventMulticaster添加到BeanFactory中,以后其他組件直接自動注入
9、onRefresh();留給子容器(子類)
?? ??? ?1、子類重寫這個方法,在容器刷新的時候可以自定義邏輯;
10、registerListeners();給容器中將所有項目里面的ApplicationListener注冊進來;
?? ??? ?1、從容器中拿到所有的ApplicationListener
?? ??? ?2、將每個監(jiān)聽器添加到事件派發(fā)器中;
?? ??? ??? ?getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
?? ??? ?3、派發(fā)之前步驟產(chǎn)生的事件;
11、finishBeanFactoryInitialization(beanFactory);初始化所有剩下的單實例bean;
?? ?1、beanFactory.preInstantiateSingletons();初始化后剩下的單實例bean
?? ??? ?1)、獲取容器中的所有Bean,依次進行初始化和創(chuàng)建對象
?? ??? ?2)、獲取Bean的定義信息;RootBeanDefinition
?? ??? ?3)、Bean不是抽象的,是單實例的,是懶加載;
?? ??? ??? ?1)、判斷是否是FactoryBean;是否是實現(xiàn)FactoryBean接口的Bean;
?? ??? ??? ?2)、不是工廠Bean。利用getBean(beanName);創(chuàng)建對象
?? ??? ??? ??? ?0、getBean(beanName); ioc.getBean();
?? ??? ??? ??? ?1、doGetBean(name, null, null, false);
?? ??? ??? ??? ?2、先獲取緩存中保存的單實例Bean。如果能獲取到說明這個Bean之前被創(chuàng)建過(所有創(chuàng)建過的單實例Bean都會被緩存起來)
?? ??? ??? ??? ??? ?從private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);獲取的
?? ??? ??? ??? ?3、緩存中獲取不到,開始Bean的創(chuàng)建對象流程;
?? ??? ??? ??? ?4、標(biāo)記當(dāng)前bean已經(jīng)被創(chuàng)建
?? ??? ??? ??? ?5、獲取Bean的定義信息;
?? ??? ??? ??? ?6、【獲取當(dāng)前Bean依賴的其他Bean;如果有按照getBean()把依賴的Bean先創(chuàng)建出來;】
?? ??? ??? ??? ?7、啟動單實例Bean的創(chuàng)建流程;
?? ??? ??? ??? ??? ?1)、createBean(beanName, mbd, args);
?? ??? ??? ??? ??? ?2)、Object bean = resolveBeforeInstantiation(beanName, mbdToUse);讓BeanPostProcessor先攔截返回代理對象;
?? ??? ??? ??? ??? ??? ?【InstantiationAwareBeanPostProcessor】:提前執(zhí)行;
?? ??? ??? ??? ??? ??? ?先觸發(fā):postProcessBeforeInstantiation();
?? ??? ??? ??? ??? ??? ?如果有返回值:觸發(fā)postProcessAfterInitialization();
?? ??? ??? ??? ??? ?3)、如果前面的InstantiationAwareBeanPostProcessor沒有返回代理對象;調(diào)用4)
?? ??? ??? ??? ??? ?4)、Object beanInstance = doCreateBean(beanName, mbdToUse, args);創(chuàng)建Bean
?? ??? ??? ??? ??? ??? ? 1)、【創(chuàng)建Bean實例】;createBeanInstance(beanName, mbd, args);
?? ??? ??? ??? ??? ??? ? ?? ?利用工廠方法或者對象的構(gòu)造器創(chuàng)建出Bean實例;
?? ??? ??? ??? ??? ??? ? 2)、applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
?? ??? ??? ??? ??? ??? ? ?? ?調(diào)用MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition(mbd, beanType, beanName);
?? ??? ??? ??? ??? ??? ? 3)、【Bean屬性賦值】populateBean(beanName, mbd, instanceWrapper);
?? ??? ??? ??? ??? ??? ? ?? ?賦值之前:
?? ??? ??? ??? ??? ??? ? ?? ?1)、拿到InstantiationAwareBeanPostProcessor后置處理器;
?? ??? ??? ??? ??? ??? ? ?? ??? ?postProcessAfterInstantiation();
?? ??? ??? ??? ??? ??? ? ?? ?2)、拿到InstantiationAwareBeanPostProcessor后置處理器;
?? ??? ??? ??? ??? ??? ? ?? ??? ?postProcessPropertyValues();
?? ??? ??? ??? ??? ??? ? ?? ?=====賦值之前:===
?? ??? ??? ??? ??? ??? ? ?? ?3)、應(yīng)用Bean屬性的值;為屬性利用setter方法等進行賦值;
?? ??? ??? ??? ??? ??? ? ?? ??? ?applyPropertyValues(beanName, mbd, bw, pvs);
?? ??? ??? ??? ??? ??? ? 4)、【Bean初始化】initializeBean(beanName, exposedObject, mbd);
?? ??? ??? ??? ??? ??? ? ?? ?1)、【執(zhí)行Aware接口方法】invokeAwareMethods(beanName, bean);執(zhí)行xxxAware接口的方法
?? ??? ??? ??? ??? ??? ? ?? ??? ?BeanNameAware\BeanClassLoaderAware\BeanFactoryAware
?? ??? ??? ??? ??? ??? ? ?? ?2)、【執(zhí)行后置處理器初始化之前】applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
?? ??? ??? ??? ??? ??? ? ?? ??? ?BeanPostProcessor.postProcessBeforeInitialization();
?? ??? ??? ??? ??? ??? ? ?? ?3)、【執(zhí)行初始化方法】invokeInitMethods(beanName, wrappedBean, mbd);
?? ??? ??? ??? ??? ??? ? ?? ??? ?1)、是否是InitializingBean接口的實現(xiàn);執(zhí)行接口規(guī)定的初始化;
?? ??? ??? ??? ??? ??? ? ?? ??? ?2)、是否自定義初始化方法;
?? ??? ??? ??? ??? ??? ? ?? ?4)、【執(zhí)行后置處理器初始化之后】applyBeanPostProcessorsAfterInitialization
?? ??? ??? ??? ??? ??? ? ?? ??? ?BeanPostProcessor.postProcessAfterInitialization();
?? ??? ??? ??? ??? ??? ? 5)、注冊Bean的銷毀方法;
?? ??? ??? ??? ??? ?5)、將創(chuàng)建的Bean添加到緩存中singletonObjects;
?? ??? ??? ??? ?ioc容器就是這些Map;很多的Map里面保存了單實例Bean,環(huán)境信息。。。。;
?? ??? ?所有Bean都利用getBean創(chuàng)建完成以后;
?? ??? ??? ?檢查所有的Bean是否是SmartInitializingSingleton接口的;如果是;就執(zhí)行afterSingletonsInstantiated();
12、finishRefresh();完成BeanFactory的初始化創(chuàng)建工作;IOC容器就創(chuàng)建完成;
?? ??? ?1)、initLifecycleProcessor();初始化和生命周期有關(guān)的后置處理器;LifecycleProcessor
?? ??? ??? ?默認從容器中找是否有l(wèi)ifecycleProcessor的組件【LifecycleProcessor】;如果沒有new DefaultLifecycleProcessor();
?? ??? ??? ?加入到容器;
?? ??? ??? ?
?? ??? ??? ?寫一個LifecycleProcessor的實現(xiàn)類,可以在BeanFactory
?? ??? ??? ??? ?void onRefresh();
?? ??? ??? ??? ?void onClose();?? ?
?? ??? ?2)、?? ?getLifecycleProcessor().onRefresh();
?? ??? ??? ?拿到前面定義的生命周期處理器(BeanFactory);回調(diào)onRefresh();
?? ??? ?3)、publishEvent(new ContextRefreshedEvent(this));發(fā)布容器刷新完成事件;
?? ??? ?4)、liveBeansView.registerApplicationContext(this);
?? ?
?? ?======總結(jié)===========
?? ?1)、Spring容器在啟動的時候,先會保存所有注冊進來的Bean的定義信息;
?? ??? ?1)、xml注冊bean;<bean>
?? ??? ?2)、注解注冊Bean;@Service、@Component、@Bean、xxx
?? ?2)、Spring容器會合適的時機創(chuàng)建這些Bean
?? ??? ?1)、用到這個bean的時候;利用getBean創(chuàng)建bean;創(chuàng)建好以后保存在容器中;
?? ??? ?2)、統(tǒng)一創(chuàng)建剩下所有的bean的時候;finishBeanFactoryInitialization();
?? ?3)、后置處理器;BeanPostProcessor
?? ??? ?1)、每一個bean創(chuàng)建完成,都會使用各種后置處理器進行處理;來增強bean的功能;
?? ??? ??? ?AutowiredAnnotationBeanPostProcessor:處理自動注入
?? ??? ??? ?AnnotationAwareAspectJAutoProxyCreator:來做AOP功能;
?? ??? ??? ?xxx....
?? ??? ??? ?增強的功能注解:
?? ??? ??? ?AsyncAnnotationBeanPostProcessor
?? ??? ??? ?....
?? ?4)、事件驅(qū)動模型;
?? ??? ?ApplicationListener;事件監(jiān)聽;
?? ??? ?ApplicationEventMulticaster;事件派發(fā):
?? ?
public void refresh() throws BeansException, IllegalStateException {
? ? ? ? synchronized (this.startupShutdownMonitor) {
? ? ? ? ? ? prepareRefresh();
? ? ? ? ? ? ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
? ? ? ? ? ? prepareBeanFactory(beanFactory);
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? postProcessBeanFactory(beanFactory);
? ? ? ? ? ? ? ? invokeBeanFactoryPostProcessors(beanFactory);
? ? ? ? ? ? ? ? registerBeanPostProcessors(beanFactory);
? ? ? ? ? ? ? ? initMessageSource();
? ? ? ? ? ? ? ? initApplicationEventMulticaster();
? ? ? ? ? ? ? ? onRefresh();
? ? ? ? ? ? ? ? registerListeners();
? ? ? ? ? ? ? ? finishBeanFactoryInitialization(beanFactory);
? ? ? ? ? ? ? ? finishRefresh();
? ? ? ? ? ? }
? ? ? ? ? ? catch (BeansException ex) {
? ? ? ? ? ? ? ? if (logger.isWarnEnabled()) {
? ? ? ? ? ? ? ? ? ? logger.warn("Exception encountered during context initialization - " +
? ? ? ? ? ? ? ? ? ? ? ? ? ? "cancelling refresh attempt: " + ex);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? destroyBeans();
? ? ? ? ? ? ? ? cancelRefresh(ex);
? ? ? ? ? ? ? ? throw ex;
? ? ? ? ? ? }
? ? ? ? ? ? finally {
? ? ? ? ? ? ? ? // Reset common introspection caches in Spring's core, since we
? ? ? ? ? ? ? ? // might not ever need metadata for singleton beans anymore...
? ? ? ? ? ? ? ? resetCommonCaches();
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
總結(jié)
以上是生活随笔為你收集整理的Spring容器创建过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: @EnableTransactionMa
- 下一篇: 数据结构中图的一些定义