这一个篇章很乱(不要习惯性说有一点)。
Major: 加载propertices文件的EnableAutoConfiguration
Preview: springboot启动类调用TomcatWebServer代码路径
Review: 回顾知识点
Major: springboot启动类,加载配置文件
获取jar包里面的配置文件
@SpringBootApplication
@EnableAutoConfiguration
@Import({AutoConfigurationImportSelector.class})
AutoConfigurationImportSelector.getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {}
this.getSpringFactoriesLoaderFactoryClass()
//mark: 启动类的配置
spring-boot-autoconfigure-2.3.7.RELEASE.jar
META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
@Import标签解析方法
SpringApplication.run(ZoneApplication.class, args);
SpringApplication.run(Class<?> primarySource, String... args) {}
SpringApplication.run(Class<?>[] primarySources, String[] args) {}
SpringApplication.ConfigurableApplicationContext run(String... args) {
this.refreshContext(context);
this.refresh((ApplicationContext)context);
this.refresh((ConfigurableApplicationContext)applicationContext);
applicationContext.refresh();
AbstractApplicationContext.refresh() throws BeansException, IllegalStateException {}
this.invokeBeanFactoryPostProcessors(beanFactory);
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, this.getBeanFactoryPostProcessors());
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {}
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
postProcessor.postProcessBeanDefinitionRegistry(registry);
ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {}
this.processConfigBeanDefinitions(registry);
parser.parse(candidates);
ConfigurationClassParser.parse(Set<BeanDefinitionHolder> configCandidates) {}
parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
processConfigurationClass(new ConfigurationClass(metadata, beanName), DEFAULT_EXCLUSION_FILTER);
// 解析各种注解
sourceClass = doProcessConfigurationClass(configClass, sourceClass, filter);
this.processImports(configClass, sourceClass, this.getImports(sourceClass), filter, true);
Preview: SpringBoot tomcat启动
SpringBoot如何启动Tomcat
SpringApplication.run(ZoneApplication.class, args);
SpringApplication.run(Class<?> primarySource, String... args) {}
return (new SpringApplication(primarySources)).run(args);
this.refreshContext(context);
this.refresh((ApplicationContext)context);
this.refresh((ConfigurableApplicationContext)applicationContext);
applicationContext.refresh();
ServletWebServerApplicationContext.refresh() throws BeansException, IllegalStateException {}
super.refresh();
this.onRefresh();
ServletWebServerApplicationContext.onRefresh() {}
super.onRefresh();
this.createWebServer();
WebServer webServer = this.webServer;
ServletContext servletContext = this.getServletContext();
最终结果,最后两行代码,注入这两个类。
TomcatServletWebServer
TomcatWebServer
Review: 6个预习知识点
1#获取两个处理类
AnnotationConfigUtils
CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME
COMMON_ANNOTATION_PROCESSOR_BEAN_NAME
2# 注册BPP,这里两个代码是一样的。
AbstractApplicationContext.prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {}
// 注册BPP
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));applicationContext
PostProcessorRegistrationDelegate.registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {}
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
// 注册ApplicationListenerDetector到beanFactory中
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
3# 记录下BeanPostProcessor的目标计数
// 记录下BeanPostProcessor的目标计数
// 此处为什么要+1呢,原因非常简单,在此方法的最后会添加一个BeanPostProcessorChecker的类
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
// 添加BeanPostProcessorChecker(主要用于记录信息)到beanFactory中
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
4# PostProcessorRegistrationDelegate.BeanPostProcessorChecker内部类new的时候注意会调用方法post,after
postProcessBeforeInitialization(Object bean, String beanName) {}
postProcessAfterInitialization(Object bean, String beanName) {}
5# BeanDefinition
Spring三种Role,仅内部使用
6# BeanPostProcessor调用和注册
AbstractApplicationContext.java
// Register bean processors that intercept bean creation.
// 注册bean处理器,这里只是注册功能,真正调用的是getBean方法
registerBeanPostProcessors(beanFactory);
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
PostProcessorRegistrationDelegate.java
// 找到所有实现了BeanPostProcessor接口的类
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
7# BeanPostProcessor.java关联类预习
BeanPostProcessor.java
postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {}
postProcessAfterInitialization(Object bean, String beanName) throws BeansException {}
InstantiationAwareBeanPostProcessor.java
postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {}
postProcessProperties(PropertyValues pvs, Object bean, String beanName)
throws BeansException {}
SmartInstantiationAwareBeanPostProcessor.java
determineCandidateConstructors(Class<?> beanClass, String beanName)
throws BeansException {}
getEarlyBeanReference(Object bean, String beanName) throws BeansException {}
AbstractAutowireCapableBeanFactory.java
getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {}
// 让exposedObject经过每个SmartInstantiationAwareBeanPostProcessor的包装
exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName)
throws BeansException {}
Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName);
MergedBeanDefinitionPostProcessor.java
postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);
resetBeanDefinition(String beanName) {}
DestructionAwareBeanPostProcessor.java
postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
requiresDestruction(Object bean) {}
CommonAnnotationBeanPostProcessor
postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {}
resetBeanDefinition(String beanName) {}