spring Ioc 容器初始化
2020-09-10 14:42:26 0 举报
spring ioc 容器初始化过程
作者其他创作
大纲/内容
添加beanPostProcessor【ApplicationContextAwareProcessor】
invokeBeanFactoryBeanPostProcessor(BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry)准备beanDefinishon
有 如下几种,在不同的时机执行 BeanPostProcessor、 DestructionAwareBeanPostProcessor【bean销毁】、 InstantiationAwareBeanPostProcessor【bean创建之后】、 SmartInstantiationAwareBeanPostProcessor、 MergedBeanDefinitionPostProcessor【internalPostProcessors】
入口类注册到beanFactory中
准备bean工厂
实际上使用的AutowiredAnnotationBeanPostProcessor来处理inject对象
尝试通过SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors来找到创建bean的构造方法
钩子
Initializate bean
BeanNameAwareBeanClassLoaderAwareBeanFactoryAware
this.beanFactory = new DefaultListableBeanFactory()
是否存在
通过遍历,get所有bean来实例化
DestructionAwareBeanPostProcessor#postProcessBeforeDestruction
通过AnnotatedBeanDefinitionReader#doRegisterBean来,将入口类解析为AnnotatedGenericBeanDefinition并注册到context中
返回bean
prepare beanFactory
applyMergedBeanDefinitionPostProcessors
使用默认构造函数
applyPropertyValues
Populate bean
注册annotation相关处理器的bean到beanDefinitionRegitray(就是现在的applicationContext对象)中,以供后续容器refresh使用0 = \"org.springframework.context.annotation.internalConfigurationAnnotationProcessor【ConfigurationClassPostProcessor.class】\"1 = \"org.springframework.context.annotation.internalAutowiredAnnotationProcessor【AutowiredAnnotationBeanPostProcessor.class】\"2 = \"org.springframework.context.annotation.internalRequiredAnnotationProcessor\"3 = \"org.springframework.context.annotation.internalCommonAnnotationProcessor【CommonAnnotationBeanPostProcessor.class】\"4 = \"org.springframework.context.event.internalEventListenerProcessor【EventListenerMethodProcessor.class】\"5 = \"org.springframework.context.event.internalEventListenerFactory【DefaultEventListenerFactory.class】\"
是
doGetBean
Instantiate non-lazy-init bean
finishedRefresh
Container
this.reader = new AnnotatedBeanDefinitionReader(this)
从cache中获取
bean结束
未创建
找到
AfterInitialization(eg : )BeanPostProcessor#postProcessAfterInitialization
注册beanPostProcessor把beanDefinishonMap中的BeanPostProcessor排序放到beanPostProcessor中,供bean创建过程使用
让InstantiationAwareBeanPostProcessors#postProcessBeforeInstantiation来尝试创建bean instance
调用doGetBean来获取要注入人的对象
new ConfigurationClassBeanDefinitionReader
1、spring在创建BeanFactory和创建bean时,都开放了扩展点给自己和第三方,比如,spring的注解解析就是使用bean创建之前的一个扩展点ClassConfigurantionBeanPostProcessor来实现的2、postProcessor分两种,beanFactoryPostProcessor和BeanPostProcessor3、bean初始化和销毁方法接口
Instantiation之后InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation
invokeAwareMethod
this.scanner = new ClassPathBeanDefinitionScanner(this)
spring内置的钩子
添加 BeanPostProcessor【ImportAwareBeanPostProcessor】
是否找到构造方法
是否返回bean对象
加载所有的beanDefinition
添加beanPostProcessor【ApplicationListenerDetector】用来当bean创建完成后,判断该bean是否是ApplicationListener,如果是,则加入到多播器中
new AnnotationApplicationContext
invokeBeanFactoryBeanPostProcessor(其它BeanFactoryPostProcessor#postProcessBeanFactory)
否
FeginClient就是一种FactoryBean
BeanPostProcessor#postProcessAfterInitialization
bean.destory
BeanClassLoaderAwareBeanFactoryAwareEnvironmentAwareResourceLoaderAware
bean销毁流程
reader.loadBeanDefinition
根据构造方法创建实力对象
创建doCreateBean
未找到
使用该FactoryBean#get创建最终bean
invokeAwareMethods
已创建
模板方法finishRefresh()
到目前位置,容器中的beanPostProcessor有0 = {ApplicationContextAwareProcessor} 1 = {ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor} 2 = {PostProcessorRegistrationDelegate$BeanPostProcessorChecker} 3 = {CommonAnnotationBeanPostProcessor} 4 = {AutowiredAnnotationBeanPostProcessor} 5 = {ApplicationListenerDetector}
解析成ConfigurationClass
createBeanInstance
是否FactoryBean
invokeInitMethods
注册bean的销毁方法如果有需要registerDisposableBeanIfNecessary
registerListener把容器中所有的ApplicationListener添加到多播器中
发布事件ContextRefreshedEvent
注册到registray中
0 条评论
下一页