1.位置
在spring-context包中
2.作用
ImportSelector 开放接口实现,提供SPI机制,@Import注解来导入。
DeferredImportSelector 延迟导入。
3.原理
3.1加载import类
ImportSelector 和DeferredImportSelector 导入的类,全部放到ConfigurationClassParser内部类ImportStack的属性:
private final MultiValueMap<String, AnnotationMetadata> imports
3.2存放延迟加载类
但是放入时机不一样,DeferredImportSelector 首次导入时,存放到ConfigurationClassParser内部类DeferredImportSelectorHandler中的属性:
private List<ConfigurationClassParser.DeferredImportSelectorHolder> deferredImportSelectors;
3.3加载延迟加载类
执行
this.deferredImportSelectorHandler.process();
将deferredImportSelector导入的类放入ConfigurationClassParser内部类ImportStack的属性:
private final MultiValueMap<String, AnnotationMetadata> imports
4.时序
SpringApplication.run(Class<?> primarySource,String... args)
->SpringApplication.run(Class<?>[] primarySources,String[] args)
->SpringApplication.run(String... args)
->SpringApplication.refreshContext(ConfigurableApplicationContext context)
->AbstractApplicationContext.refresh()
->AbstractApplicationContext.invokeBeanFactoryPostProcessors
->PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
->ConfigurationClassPostProcessor.invokeBeanDefinitionRegistryPostProcessors
->ConfigurationClassPostProcessor.processConfigBeanDefinitions
->ConfigurationClassParser.parse(Set<BeanDefinitionHolder> configCandidates)
->ConfigurationClassParser.parse(AnnotationMetadata metadata, String beanName)
->ConfigurationClassParser.processConfigurationClass
->ConfigurationClassParser.doProcessConfigurationClass
->this.processImports
->ConfigurationClassParser.processImports递归调用
->ImportSelector.selectImports
->this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector)selector);
->this.importStack.registerImport
->this.deferredImportSelectorHandler.process();
->DeferredImportSelectorGroupingHandler.register
->DeferredImportSelectorGroupingHandler.processGroupImports()
->ConfigurationClassParser.this.processImports
->this.importStack.registerImport(currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
网友评论