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