前面我们摸清楚了整个invokeBeanFactoryPostProcessors方法的if-else逻辑和3个for循环的核心脉络逻辑。
接下来我们来看下细节,我会通过抓大放小的思想,带大家看到在扩展点执行的过程中,最最要的有哪一些。
SpringBoot的自动装配配置如何做到的、第三方技术如何进行扩展的。
SpringBoot的自动装配配置如何做到的?
if-else逻辑中哪些BeanFactoryPostProcessor执行了?
之前我们提到过,invokeBeanFactoryPostProcessors执行的BeanFactoryPostProcessor主要来源是容器的两个属性 beanFactoryPostProcessors 和BeanDefinitionMap。
首先这两个属性,会在之前执行扩展操作,比如listener或者initializer的方法时,设置进去值的。执行到invokeBeanFactoryPostProcessors时,之前会设置如下图所示的值:
从上图可以看出来,执行invokeBeanFactoryPostProcessors的时候已经有4个BeanFactoryPostProcessor。
当执行invokeBeanFactoryPostProcessors,核心脉络上一节我们分析出了是主要一个if-else+3个for循环组成的,这个if-else中有分了内部的、实现PriorityOrderd、Ordered、NonOrder这个四个顺序执行。结合上面4个BeanFactoryPostProcessor,整体执行如下图所示:
从图中可以看出来,非常关键的一点那就是:在执行扩展方法1的过程中,通过Spring内部的一个ConfigurationClassPostProcessor,补充了新的BeanDefinition,增加了新的BeanFactoryPostProcessor。
ConfigurationClassPostProcessor这个执行非常关键,因为它补充了新的BeanDefinition。
它核心用来进行加载ClassPath下所有java注解定义的BeanDefinition。比如:自己包下定义的@Service,@Component,@Controller@Configuration @Bean等注解定义的Bean,也包括外部的starter中@Configuration @Bean等配置。
也就是你定义的大多数bean和外部starter定义的Bean的BeanDefinition都会被放入到容器中。
另外,补充了新的BeanDefinition,这里我们简化了下,假设当前应用,只依赖了一个myBatis-starter,之后只会补充一个MyBatis相关的BeanDefinition,一个BeanFactoryPostProcessor—MapperScannerConfigurer。从名字上猜测,它应该是用来扫描MyBatis相关bean的。
invokeBeanFactoryPostProcessors的if-else逻辑中,触发了2个扩展操作,最后还会执行扩展方法2,之前的所有BeanFactoryPostProcessor,统一会执行扩展方法2。
扩展方法2执行的逻辑,基本没有什么核心的,这里我们就直接过了,你知道invokeBeanFactoryPostProcessors这里会触发这个扩展点,并且在扩展方法1之后执行就行了。
最终执行完if-else后,BeanFactory中的主要有如下的beanDefination:
beanDefinitionNames = {ArrayList@3752} size = 164
0 = "org.springframework.context.annotation.internalConfigurationAnnotationProcessor"
1 = "org.springframework.context.annotation.internalAutowiredAnnotationProcessor"
2 = "org.springframework.context.annotation.internalCommonAnnotationProcessor"
3 = "org.springframework.context.event.internalEventListenerProcessor"
4 = "org.springframework.context.event.internalEventListenerFactory"
5 = "learnSpringBootApplication"
6 = "org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory"
7 = "userController"
8 = "myBeanPostProcessor"
9 = "userServiceImpl"
10 = "userMapper"
11 = "org.springframework.boot.autoconfigure.AutoConfigurationPackages"
12 = "org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration"
13 = "propertySourcesPlaceholderConfigurer"
14 = "org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration"
15 = "websocketServletWebServerCustomizer"
16 = "org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration"
17 = "org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat"
18 = "tomcatServletWebServerFactory"
19 = "org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration"
20 = "servletWebServerFactoryCustomizer"
21 = "tomcatServletWebServerFactoryCustomizer"
22 = "org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor"
23 = "org.springframework.boot.context.internalConfigurationPropertiesBinderFactory"
24 = "org.springframework.boot.context.internalConfigurationPropertiesBinder"
25 = "org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator"
26 = "org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata"
27 = "server-org.springframework.boot.autoconfigure.web.ServerProperties"
28 = "webServerFactoryCustomizerBeanPostProcessor"
29 = "errorPageRegistrarBeanPostProcessor"
30 = "org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration"
31 = "dispatcherServlet"
32 = "spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties"
33 = "spring.http-org.springframework.boot.autoconfigure.http.HttpProperties"
34 = "org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration"
35 = "dispatcherServletRegistration"
36 = "org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration"
37 = "org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration"
38 = "taskExecutorBuilder"
39 = "applicationTaskExecutor"
40 = "spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties"
41 = "org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration"
42 = "defaultValidator"
43 = "methodValidationPostProcessor"
44 = "org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration"
45 = "error"
46 = "beanNameViewResolver"
47 = "org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration"
48 = "conventionErrorViewResolver"
49 = "org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration"
50 = "errorAttributes"
51 = "basicErrorController"
52 = "errorPageCustomizer"
53 = "preserveErrorControllerTargetClassPostProcessor"
54 = "spring.resources-org.springframework.boot.autoconfigu