1.SpringApplicationContext 初始化阶段 -> 加载依赖jar包内所有String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";(SpringFactories)文件,
保存到内存,即SpringFactoriesLoader cache属性
2.SpringApplicationContext初始化完成,执行run方法,
2.1 creatContext: webapplication type -> SERVERLET;return ConfigurableApplicationContext ;
2.2 prepareContext: 找到main函数所在类,装配到BeanDefineRegistery
2.3 refreshContext:
2.3.1 AbstractApplicationContext.refresh() ->
2.3.2 PostProcessorRegistrationDelegate invokeBeanFactoryPostProcessors ->invokeBeanDefinitionRegistryPostProcessors() -> postProcessor.postProcessBeanDefinitionRegistry(registry);
2.3.3 ConfigurationClassPostProcesser.postProcessBeanDefinitionRegistry(registry) -> processConfigBeanDefinitions(registry);
2.3.3.1 processConfigBeanDefinitions(registry);中获取candidates(元素类型:BeanDefinitionHolder,main函数所在Class)
2.3.3.2 parser.parse(candidates) -> processConfigurationClass(new ConfigurationClass(metadata,beanName)) -> getImports() ->this.group.process()
-> processImports() -> Class<?> candidateClass = candidate.loadClass(); ->
2.3.3.3 AutoConfigurationImportSelector.process()
(解释:引入mybatis未写配置,会在refreshContext(context)报错,因为是先autoconfigure加载spring.factories配置后会执行到2.3.3.3,在依赖datasourceProperties时,未找到attribute,所以报错!)
2.3.4 mybatis-plus-starter(待补充)
springboot自动装配原理-整理
最新推荐文章于 2024-08-10 21:31:48 发布