spring启动流程
2022-12-16 10:20:53 0 举报
spring启动流程代码描述
作者其他创作
大纲/内容
AnnotationConfigApplicationContext(Class<?>... componentClasses)
this()
AnnotationConfigApplicationContext()
this.reader = new AnnotatedBeanDefinitionReader(this);
registerAnnotationConfigProcessors
创建DefaultListableBeanFactory
设置beanFactory的OrderComparator为AnnotationAwareOrderComparator
用来判断某个Bean能不能用来进行依赖注入
注册ConfigurationClassPostProcessor类型的BeanDefinition
注册AutowiredAnnotationBeanPostProcessor类型的BeanDefinition
注册CommonAnnotationBeanPostProcessor类型的BeanDefinition
注册EventListenerMethodProcessor类型的BeanDefinition,用来处理@EventListener注解的
注册DefaultEventListenerFactory类型的BeanDefinition,用来处理@EventListener注解的
this.scanner = new ClassPathBeanDefinitionScanner(this);
setEnvironment
setResourceLoader
registerDefaultFilters
注册@Component对应的AnnotationTypeFilter
register(componentClasses)
new AnnotatedGenericBeanDefinition()
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
refresh();
prepareRefresh()
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()
这里会判断能否刷新,并且返回一个BeanFactory,.
刷新不代表完全情况,主要是先执行Bean的销毁,然后重新生成一个BeanFactory,再在接下来的步骤中重新去扫描等等
这里会判断能否刷新,并且返回一个BeanFactory,.
刷新不代表完全情况,主要是先执行Bean的销毁,然后重新生成一个BeanFactory,再在接下来的步骤中重新去扫描等等
prepareBeanFactory(beanFactory)
Spring5.3中新增的功能,可以选择是否开启Spel功能,shouldIgnoreSpel默认为false,表示开启
添加一个ResourceEditorRegistrar,注册一些级别的类型转化器
组成一个BeanPostProcessor,用来处理EnvironmentAware、EmbeddedValueResolverAware等回调
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
如果一个属性对应的set方法在ignoredDependencyInterfaces接口中被定义了,则该属性不会进行自动注入(是Spring中的自动注入,不是@Autowired)
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(ApplicationStartupAware.class);
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(ApplicationStartupAware.class);
ApplicationListenerDetector负责把ApplicantsListener类型的Bean注册到ApplicationContext中
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
Aspectj本身是通过编译期进行代理的,在Spring中就跟LoadTimeWeaver有关
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
postProcessBeanFactory(beanFactory)
子类去实现,注册需要的beanFactoryPostProcess
invokeBeanFactoryPostProcessors(beanFactory)
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
在BeanFactory中找到类型为BeanDefinitionRegistryPostProcessor的beanName,
也是ConfigurationClassPostProcessor, 调用BeanFactory的getBean方法得到实例对象
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
也是ConfigurationClassPostProcessor, 调用BeanFactory的getBean方法得到实例对象
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry
解析配置类Appconfig
扫描得到beanDefintion,并注册
扫描@Import,@bean得到beanDifintion,并注册
执行其他BeanDefinitionRegistryPostProcessolpostProcessBeanDefinitionRegistry0方法
执行所有BeanDefinitionRegistryPostProcessore;postProcessBeanFactory0方法
从BeanFactory中找到誉型为BeanFactoryPostProcessoregbeanName
这些BeanFactoryPostProcessor包括了上面的BeanDefinitionRegistryPostProcessor
这些BeanFactoryPostProcessor包括了上面的BeanDefinitionRegistryPostProcessor
执行还没有执行过的
BeanFactoryPostProcessor.postProcessBeanFactory0方法
BeanFactoryPostProcessor.postProcessBeanFactory0方法
到此,所有的BeanFactoryPostProcessor的逻辑都执行完了,主要做的事情就是得到BeanDefinition并注册到BeanFactory中
registerBeanPostProcessors(beanFactory)
registerBeanPostProcessorsibeanFactory();因上面的步骤完成了扫描。
这个过程中程序员可自己定义了一些BeanPostProcessor,在这步就会把BeanFactory中所有的BeanPostProcessor找出来并实例化得到一个对象,并添加到BeanFactory中去 (属性beanPostProcessors),最后直接添加一ApplicationListenerDetector对象(也是一个BeanPostProcessorl)
使得ApplicationListenerDetectorBeanPostProcessor列表中的最后BeanPostProcessor
这个过程中程序员可自己定义了一些BeanPostProcessor,在这步就会把BeanFactory中所有的BeanPostProcessor找出来并实例化得到一个对象,并添加到BeanFactory中去 (属性beanPostProcessors),最后直接添加一ApplicationListenerDetector对象(也是一个BeanPostProcessorl)
使得ApplicationListenerDetectorBeanPostProcessor列表中的最后BeanPostProcessor
initMessageSource()
如果BeanFactory中存在个做"messagesource"的BeanDefinition那么就会把这个Bean对象创建出来并赋值给ApplicationContext的messageSource属性,让AoplicationContext拥有国际化的功能
initApplicationEventMulticaster()
如果BeanFactory中存在一个叫做"aoplicationEventMulticaster"的BeanDefinition,那么就会把这个Bean对象创建出来并赋值给AoplicationContext的applicationEventMulticaster属性,让ApplicationContext拥有事件发布的功能
onRefresh()
提供给AbstractApplicationContext的子类进行扩展
registerListeners()
从BeanFactory中获取ApplicationListener类型的beanName,然后添加到ApplicationContext中的事件广播器applicationEventMulticaster中去
finishBeanFactoryInitialization(beanFactory)
finishBeanFactorylnitialization(beanFactory)完成BeanFactory的初始化,主要就是实例化非懒加载的单例Bean
finishRefresh()BeanFactory的初始化完后,就到了spring启动的最后一步了
设置ApplicationContext的lifecycleProcessor
执行LifecycleBean
发布ContextRefreshedEvent事件
收藏
0 条评论
下一页