spring容器加载
2020-03-12 17:00:23 44 举报
AI智能生成
spring 容器初始化分解
作者其他创作
大纲/内容
构造
new AnnotationConfigApplicationContext()
new AnnotationConfigApplicationContext()
DefaultResourceLoader->
this.classLoader = ClassUtils.getDefaultClassLoader();
this.classLoader = ClassUtils.getDefaultClassLoader();
AbstractApplicationContext->this.resourcePatternResolver = getResourcePatternResolver();
GenericApplicationContext->
this.beanFactory = new DefaultListableBeanFactory();
初始化构造beanFactory
this.beanFactory = new DefaultListableBeanFactory();
初始化构造beanFactory
AbstractAutowireCapableBeanFactory->ignoreDependencyInterface(BeanNameAware.class);
容器注册时,忽略实现BeanNameAware接口,setXXX方法的自动注入
容器注册时,忽略实现BeanNameAware接口,setXXX方法的自动注入
AbstractAutowireCapableBeanFactory->ignoreDependencyInterface(BeanFactoryAware.class);
容器注册时,忽略实现BeanFactoryAware接口,setXXX方法的自动注入
容器注册时,忽略实现BeanFactoryAware接口,setXXX方法的自动注入
AbstractAutowireCapableBeanFactory->ignoreDependencyInterface(BeanClassLoaderAware.class);
容器注册时,忽略实现BeanClassLoaderAware接口,setXXX方法的自动注入
容器注册时,忽略实现BeanClassLoaderAware接口,setXXX方法的自动注入
AnnotationConfigApplicationContext->
this.reader = new AnnotatedBeanDefinitionReader(this)
创建基于注解的BeanDefinition读取器
this.reader = new AnnotatedBeanDefinitionReader(this)
创建基于注解的BeanDefinition读取器
registerAnnotationConfigProcessors
注册注解配置相关的处理器BeanDefinition
注册注解配置相关的处理器BeanDefinition
【ConfigurationClassPostProcessor(BeanDefinitionRegistryPostProcessor->BeanFactoryPostProcessor)】
【AutowiredAnnotationBeanPostProcessor(BeanPostProcessor)】
【CommonAnnotationBeanPostProcessor(BeanPostProcessor)】
.org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor(JPA相关)
【EventListenerMethodProcessor(BeanFactoryPostProcessor)】
【DefaultEventListenerFactory(EventListenerFactory)】
核心代码
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, xxxxName))
==>registry.registerBeanDefinition(beanName, definition);
====>this.beanDefinitionMap.put(beanName, beanDefinition);
====>this.beanDefinitionNames.add(beanName);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, xxxxName))
==>registry.registerBeanDefinition(beanName, definition);
====>this.beanDefinitionMap.put(beanName, beanDefinition);
====>this.beanDefinitionNames.add(beanName);
AnnotationConfigApplicationContext->
this.scanner = new ClassPathBeanDefinitionScanner(this)
创建BeanDefinition扫描器
this.scanner = new ClassPathBeanDefinitionScanner(this)
创建BeanDefinition扫描器
ClassPathBeanDefinitionScanner->setEnvironment(environment);
ClassPathBeanDefinitionScanner->setResourceLoader(resourceLoader)
register(componentClasses)
AnnotationConfigApplicationContext->
this.reader.register(componentClasses);
将定义配置类,注入BeanDefinition容器中
this.reader.register(componentClasses);
将定义配置类,注入BeanDefinition容器中
registerBean
doRegisterBean
processCommonDefinitionAnnotations
处理通用注解
处理通用注解
AnnotationConfigUtils.applyScopedProxyMode
BeanDefinitionReaderUtils.registerBeanDefinition
===>registry.registerBeanDefinition
===>registry.registerBeanDefinition
refresh
prepareRefresh
刷新前预处理
刷新前预处理
this.closed.set(false);
this.active.set(true);
this.active.set(true);
initPropertySources();
初始化属性设置,空方法提供子类重写
初始化属性设置,空方法提供子类重写
getEnvironment().validateRequiredProperties();
检验属性的合法等
检验属性的合法等
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
初始化早期事件监听器
初始化早期事件监听器
this.earlyApplicationEvents = new LinkedHashSet<>();
初始化早期事件集合
初始化早期事件集合
ConfigurableListableBeanFactory beanFactory
= obtainFreshBeanFactory();
获取最新的BeanFactory
= obtainFreshBeanFactory();
获取最新的BeanFactory
refreshBeanFactory
设置Id
getBeanFactory
prepareBeanFactory(beanFactory);
BeanFactory的预准备工作
对BeanFactory进行一些设置
BeanFactory的预准备工作
对BeanFactory进行一些设置
beanFactory.setBeanClassLoader(getClassLoader())
设置类加载器
设置类加载器
beanFactory.setBeanExpressionResolver
设置表达式解析器
设置表达式解析器
beanFactory.addPropertyEditorRegistrar
beanFactory.addBeanPostProcessor
添加Bean后置处理器
添加Bean后置处理器
【ApplicationContextAwareProcessor(BeanPostProcessor)】
beanFactory.ignoreDependencyInterface
容器注册时,忽略实现接口,setXXX方法的自动注入
容器注册时,忽略实现接口,setXXX方法的自动注入
EnvironmentAware
ResourceLoaderAware
EmbeddedValueResolverAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationContextAware
beanFactory.registerResolvableDependency
容器注册时,实现以下接口,字段or参数支持自动注入
容器注册时,实现以下接口,字段or参数支持自动注入
BeanFactory
ResourceLoader
ApplicationEventPublisher
ApplicationContext
beanFactory.addBeanPostProcessor
添加Bean后置处理器
添加Bean后置处理器
【ApplicationListenerDetector(BeanPostProcessor)】
beanFactory.registerSingleton
ioc容器中,注入组件实例
==>this.singletonObjects.put(beanName, singletonObject);
ioc容器中,注入组件实例
==>this.singletonObjects.put(beanName, singletonObject);
getEnvironment()
getEnvironment().getSystemProperties()
getEnvironment().getSystemEnvironment()
postProcessBeanFactory(beanFactory);
BeanFactory前处理,空方法.有子类实现
BeanFactory前处理,空方法.有子类实现
invokeBeanFactoryPostProcessors(beanFactory);
执行BeanFactory后置处理器
执行BeanFactory后置处理器
判断beanFactory instanceof BeanDefinitionRegistry
获取beanFactoryPostProcessors
BeanDefinitionRegistryPostProcessor处理
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
获取处理器,基类BeanFactoryPostProcessor
获取处理器,基类BeanFactoryPostProcessor
排序:PriorityOrdered->Ordered->其他
invokeBeanDefinitionRegistryPostProcessors
==>postProcessor.postProcessBeanDefinitionRegistry(registry);
==>postProcessor.postProcessBeanDefinitionRegistry(registry);
ConfigurationClassPostProcessor
@ComponentScan注解处理
@Import注解出来
@Bean注解处理
invokeBeanFactoryPostProcessors
==>postProcessor.postProcessBeanFactory(beanFactory);
==>postProcessor.postProcessBeanFactory(beanFactory);
BeanFactoryPostProcessor处理
invokeBeanFactoryPostProcessors
==>postProcessor.postProcessBeanFactory(beanFactory);
==>postProcessor.postProcessBeanFactory(beanFactory);
EventListenerMethodProcessor
registerBeanPostProcessors(beanFactory);
注册Bean后置处理器
注册Bean后置处理器
beanFactory.getBean(ppName, BeanPostProcessor.class);
beanFactory.addBeanPostProcessor(postProcessor)
==>this.beanPostProcessors.add(beanPostProcessor);
beanFactory.addBeanPostProcessor(postProcessor)
==>this.beanPostProcessors.add(beanPostProcessor);
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
initMessageSource();
initApplicationEventMulticaster();
初始化事件多播器
初始化事件多播器
onRefresh();
初始化bean,空方法,提供子类实现.
初始化bean,空方法,提供子类实现.
registerListeners();
注册监听器
注册监听器
1、从容器中拿到所有的ApplicationListener
2、将每个监听器添加到事件派发器中;
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
3、派发之前步骤产生的事件;
finishBeanFactoryInitialization(beanFactory);
preInstantiateSingletons
初始化后剩下的单实例bean
初始化后剩下的单实例bean
遍历所有this.beanDefinitionNames
getBean(beanName);
Bean不是抽象的,是单实例的,不是懒加载
getBean(beanName);
Bean不是抽象的,是单实例的,不是懒加载
doGetBean
Object sharedInstance = getSingleton(beanName);
this.singletonObjects.get(beanName);
this.earlySingletonObjects.get(beanName);
this.singletonFactories.get(beanName);
sharedInstance为空,则开始创建bean
markBeanAsCreated(beanName); 标记创建状态
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); 获取BeanDefinition
String[] dependsOn = mbd.getDependsOn();
获取当前Bean依赖的其他Bean;如果有按照getBean()把依赖的Bean先创建出来;
获取当前Bean依赖的其他Bean;如果有按照getBean()把依赖的Bean先创建出来;
createBean
mbdToUse.prepareMethodOverrides();准备覆盖方法
resolveBeforeInstantiation
解析实例化前处理
【InstantiationAwareBeanPostProcessor】
解析实例化前处理
【InstantiationAwareBeanPostProcessor】
【InstantiationAwareBeanPostProcessor】->
applyBeanPostProcessorsBeforeInstantiation
applyBeanPostProcessorsBeforeInstantiation
【InstantiationAwareBeanPostProcessor】->
applyBeanPostProcessorsAfterInitialization
applyBeanPostProcessorsAfterInitialization
doCreateBean
instanceWrapper = createBeanInstance(beanName, mbd, args);
applyMergedBeanDefinitionPostProcessors
【MergedBeanDefinitionPostProcessor】
【MergedBeanDefinitionPostProcessor】
【MergedBeanDefinitionPostProcessor】->
postProcessMergedBeanDefinition
postProcessMergedBeanDefinition
populateBean(beanName, mbd, instanceWrapper);
bean属性赋值
bean属性赋值
【InstantiationAwareBeanPostProcessor】->
postProcessAfterInstantiation
postProcessAfterInstantiation
【InstantiationAwareBeanPostProcessor】->
postProcessPropertyValues
postProcessPropertyValues
====赋值前
applyPropertyValues开始赋值
initializeBean(beanName, exposedObject, mbd);
invokeAwareMethods
BeanNameAware->setBeanName
BeanClassLoaderAware->setBeanClassLoader
BeanFactoryAware->setBeanFactory
applyBeanPostProcessorsBeforeInitialization
遍历执行【BeanPostProcessor】->
postProcessBeforeInitialization
postProcessBeforeInitialization
invokeInitMethods
InitializingBean->afterPropertiesSet
invokeCustomInitMethod
自定义初始化方法
自定义初始化方法
applyBeanPostProcessorsAfterInitialization
遍历执行【BeanPostProcessor】->
postProcessAfterInitialization
postProcessAfterInitialization
registerDisposableBeanIfNecessary
注册Bean的销毁方法
注册Bean的销毁方法
添加至缓存
addSingleton(beanName, singletonObject);
==>this.singletonObjects.put(beanName, singletonObject);
==>this.singletonFactories.remove(beanName);
==>this.earlySingletonObjects.remove(beanName);
==>this.registeredSingletons.add(beanName);
addSingleton(beanName, singletonObject);
==>this.singletonObjects.put(beanName, singletonObject);
==>this.singletonFactories.remove(beanName);
==>this.earlySingletonObjects.remove(beanName);
==>this.registeredSingletons.add(beanName);
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
遍历已创建bean,执行SmartInitializingSingleton->afterSingletonsInstantiated
finishRefresh();
clearResourceCaches();
清理资源缓存
清理资源缓存
initLifecycleProcessor();
初始化和生命周期有关的后置处理器
初始化和生命周期有关的后置处理器
默认从容器中找是否有lifecycleProcessor的组件【LifecycleProcessor】;
如果没有new DefaultLifecycleProcessor();加入到容器;
如果没有new DefaultLifecycleProcessor();加入到容器;
getLifecycleProcessor().onRefresh();
拿到前面定义的生命周期处理器(BeanFactory);
回调onRefresh();
回调onRefresh();
publishEvent(new ContextRefreshedEvent(this));
发布容器刷新完成事件;
发布容器刷新完成事件;
LiveBeansView.registerApplicationContext(this);
resetCommonCaches();
0 条评论
下一页