SpringBoot-AutoConfiguration
2020-11-10 10:59:12 0 举报
Spring Boot 启动过程图解
作者其他创作
大纲/内容
span style=\"font-size: inherit;\
invokeBeanFactoryPostProcessors方法实现SpringBoot自动装配
依据Bean的Scope放入不同的容器
process @PropertySource
Annotationn
this.deferredImportSelectorHandler.process()
process @ImportResource
把BD注册到beanDefinitionMap
....
public @interface ComponentScan { String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class; ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT; String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN; boolean useDefaultFilters() default true; Filter[] includeFilters() default {}; Filter[] excludeFilters() default {}; boolean lazyInit() default false;}
SuperClass
RequestScope
BeanDefinitionReader
...
invokeBeanDefinitionRegistryPostProcessors
ConfigurationClassPostProcessor
registerListeners()
调用函数RefreshContext()进入IOC container
ConfigurationClassParser
public @interface ImportResource { String[] locations() default {}; Class<? extends BeanDefinitionReader> reader() default BeanDefinitionReader.class;}
invokeBeanFactoryPostProcessors
SpringApplication
BeanPostProcessor: before()
instialize Bean: doGetBean()
initApplicationEventMulticaster()
每个configurationClass如何注册BD到beanFactory的?1. 注册configurationClass本身2.注册configurationClass所包含的@Bean method3.注册@ImportResource中的locations(本质是一些.class文件)4.调用configurationClass的@Import中的那些 ImportBeanDefinitionRegistrar实现的类的zhi'xingregisterBeanDefinitions方法来注册BD.
SessionScope
BeanFactory
完整Bean对象
configCandidates:在BeanDefinition中筛选出包含有@Configuration的BD,或者和父注解中包含有@Configuration的BD:WhatsappApplication将BD包装成ConfigurationClass candidate传入
Process superclass
process @ComponentScan
DataBase
finishBeanFactoryInitialization(beanFactory)
process @Bean methods
ConfigurationClassPostProcessor::postProcessBeanFactory
XML
public @interface PropertySource { String name() default \"\"; //the name of this property source String[] value(); boolean ignoreResourceNotFound() default false; String encoding() default \"\"; Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;}
创建并启动Tomcat Sever
RemoteServer
singletonObjects
ConfigurationClassPostProcessor1. 找到beanFactory的所有@Configuration,然后用ConfigurationClassEnhancer来增强2.添加BeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory))
把candidate(是一个ConfigurationClass)中之前保存过的 DeferredImportSelector进行处理
process Member classes for @Component class
读取@Bean 修饰的方法,然后将这些method添加到candidate(是一个ConfigurationClass)
Process default methods on interfaces
ConfigurationClassPostProcessor::postProcessBeanDefinitionRegistry该方法就是在即将执行postProcessBeanFactory的时候能加载更多的BeanDefinitionLiang: 每一个bfpp执行该方法时,都是处理特定种类的BD,这个类是用来处理@Configuration的BD
beanDefinitionMap
OnRefresh()
invokeInitMethods()
populateBean
IOC container
BeanPostProcessor: after()
0 条评论
下一页