1. ApplicationContext 对象构建时登记 6 个 bean 定义
登记时机
AnnotationConfigEmbeddedWebApplicationContext.AnnotationConfigEmbeddedWebApplicationContext
-> new AnnotatedBeanDefinitionReader(this)
-> AnnotatedBeanDefinitionReader.AnnotatedBeanDefinitionReader(registry,environment)
-> AnnotationConfigUtils.registerAnnotationConfigProcessors(registry)
- 对于完全采用缺省配置的Springboot Web应用,其
ApplicationContext
的实现类最终是
AnnotationConfigEmbeddedWebApplicationContext
,该类位于包org.springframework.boot.context.embedded
。 - 该类
AnnotationConfigEmbeddedWebApplicationContext
在构造函数中会创建一个AnnotatedBeanDefinitionReader
对象用于读取注解方式的Bean定义。 - 而这个类
AnnotatedBeanDefinitionReader
在其构造函数中,又进一步调用AnnotationConfigUtils.registerAnnotationConfigProcessors(registry)
一次性登记了6个Bean定义。
6个bean定义的名称
类名 | Bean 名称(共同前缀:org.springframework.context. ) |
---|---|
ConfigurationClassPostProcessor | annotation.internalConfigurationAnnotationProcessor |
AutowiredAnnotationBeanPostProcessor | annotation.internalAutowiredAnnotationProcessor |
RequiredAnnotationBeanPostProcessor | annotation.internalRequiredAnnotationProcessor |
CommonAnnotationBeanPostProcessor | annotation.internalCommonAnnotationProcessor |
EventListenerMethodProcessor | event.internalEventListenerProcessor |
DefaultEventListenerFactory | event.internalEventListenerFactory |
6个bean定义的作用
类名 | 类型 | 作用简介 |
---|---|---|
ConfigurationClassPostProcessor | BeanPostProcessor BeanDefinitionRegistryPostProcessor | 应用启动过程中对@Configuration 配置类的处理 |
AutowiredAnnotationBeanPostProcessor | BeanPostProcessor SmartInstantiationAwareBeanPostProcessor MergedBeanDefinitionPostProcesso rPriorityOrdered | 自动装配(autowire )被注解的属性,set 方法或其他任意的配置方法。支持的注解 : @Autowared , @Value , JSR-330 @Inject |
RequiredAnnotationBeanPostProcessor | BeanPostProcessor SmartInstantiationAwareBeanPostProcessor MergedBeanDefinitionPostProcessor PriorityOrdered | 处理注解 @Required |
CommonAnnotationBeanPostProcessor | BeanPostProcessor InstantiationAwareBeanPostProcessor DestructionAwareBeanPostProcessor MergedBeanDefinitionPostProcessor PriorityOrdered | 支持通用Java 注解,尤其是JSR-250 注解,也就是javax.annotation 包内的那些注解。比如 @PostConstruct , @PreDestroy ,@Resource 和@WebServiceRef |
EventListenerMethodProcessor | SmartInitializingSingleton | 将@EventListener 注解的方法作为单个ApplicationListener 实例注册 |
DefaultEventListenerFactory | EventListenerFactory Ordered | EventListenerFactory 的缺省实现,支持所有常规的@EventListener 注解 |
2.当前应用主程序登记为第7个bean定义
登记时机
SpringApplication.run()
->prepareContext()
->BeanDefinitionLoader.load(Class<?>)
->AnnotatedBeanDefinitionReader.register(Class<?>... annotatedClasses)
->BeanDefinitionReaderUtils.registerBeanDefinition()
Bean 名字采用主程序类的短类名,也就是 application
3.登记第8个bean
登记时机
SpringApplication.run()
->refreshContext(applicationContext)
->EmbeddedWebApplicationContext.refresh()
->AbstractApplicationContext.refresh()
->AbstractApplicationContext.invokeBeanFactoryPostProcessors(beanFactory)
-> PostProcessorRegistrationDelegate
.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())
这里会调用
BeanFactoryPostProcessor
中所有的BeanDefinitionRegistryPostProcessor
的方法postProcessBeanDefinitionRegistry
,而SharedMetadataReaderFactoryContextInitializer
的内部类CachingMetadataReaderFactoryPostProcessor
就是这里的一个BeanDefinitionRegistryPostProcessor
, 它在执行时会注册第8个bean :
类名 | Bean名称 |
---|---|
SharedMetadataReaderFactoryBean | org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory |
4.其他bean
定义的添加
因为主程序上注解了 @SpringBootApplication
,而这个注解隐含了另外一个注解
@SpringBootApplication
=> @EnableAutoConfiguration
=> @Import(EnableAutoConfigurationImportSelector.class)
而作为BeanDefinitionRegistryPostProcessor
注册的一个ConfigurationClassPostProcessor
实例被执行时,也就是其方法 processConfigBeanDefinitions()
被调用时,它会基于容器中当前的Bean集合,将这些Bean上的注解@Component
,@Import
,@Configuration
作为线索查找更多的Bean定义,直到所有的Bean定义被登记到容器。
登记时机
SpringApplication.run()
->refreshContext(applicationContext)
->EmbeddedWebApplicationContext.refresh()
->AbstractApplicationContext.refresh()
->AbstractApplicationContext.invokeBeanFactoryPostProcessors(beanFactory)
-> PostProcessorRegistrationDelegate
.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())
-> ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry()
-> processConfigBeanDefinitions()
-> ConfigurationClassBeanDefinitionReader.loadBeanDefinitions()
经过上面的ConfigurationClassPostProcessor
的Bean定义发现,容器中又登记了102个Bean定义,Bean的总数达到110个,如下表所示 :
注册顺序 | Bean名称 |
---|---|
0 | org.springframework.context.annotation.internalConfigurationAnnotationProcessor |
1 | org.springframework.context.annotation.internalAutowiredAnnotationProcessor |
2 | org.springframework.context.annotation.internalRequiredAnnotationProcessor |
3 | org.springframework.context.annotation.internalCommonAnnotationProcessor |
4 | org.springframework.context.event.internalEventListenerProcessor |
5 | org.springframework.context.event.internalEventListenerFactory |
6 | application 开发人员自己的入口主程序 |
7 | org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory |
8 | sampleController 开发人员自己提供的Web控制器类 |
9 | org.springframework.boot.autoconfigure.AutoConfigurationPackages 开发人员自己的入口主程序上隐含了注解@AutoConfigurationPackage 导致入口主程序所在包被注册为该bean |
10 | org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration |
11 | org.springframework.boot.autoconfigure.condition.BeanTypeRegistry |
12 | propertySourcesPlaceholderConfigurer |
13 | org.springframework.boot.autoconfigure.jackson. JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration |
14 | standardJacksonObjectMapperBuilderCustomizer |
15 | spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties |
16 | org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor |
17 | org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.store |
18 | org.springframework.boot.autoconfigure.jackson. JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration |
19 | jacksonObjectMapperBuilder |
20 | org.springframework.boot.autoconfigure.jackson. JacksonAutoConfiguration$JacksonObjectMapperConfiguration |
21 | jacksonObjectMapper |
22 | org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration |
23 | jsonComponentModule |
24 | org.springframework.boot.autoconfigure.websocket. WebSocketAutoConfiguration$TomcatWebSocketConfiguration |
25 | websocketContainerCustomizer |
26 | org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration |
27 | org.springframework.boot.autoconfigure.web. EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat 嵌入式tomcat servlet容器的工厂配置类(@Configuration) 通过spring-boot-autoconfigure引入 因为spring-boot-starter-tomcat的存在被应用 |
28 | tomcatEmbeddedServletContainerFactory 嵌入式tomcat servlet容器的工厂 通过spring-boot-autoconfigure引入 因为spring-boot-starter-tomcat的存在被应用 |
29 | org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration 嵌入式servlet容器的自动配置类(@Configuration) 通过spring-boot-autoconfigure引入 根据实际依赖于哪个servlet容器实现包决定应用哪个servlet容器实现类 |
30 | embeddedServletContainerCustomizerBeanPostProcessor BeanPostProcessor,用来对ConfigurableEmbeddedServletContainer应用 所有bean容器中注册的EmbeddedServletContainerCustomizer 由自动配置类EmbeddedServletContainerAutoConfiguration注册 |
31 | errorPageRegistrarBeanPostProcessor 由自动配置类EmbeddedServletContainerAutoConfiguration注册 |
32 | org.springframework.boot.autoconfigure.web. DispatcherServletAutoConfiguration$DispatcherServletConfiguration 用于提供dispatcherServlet bean定义 通过spring-boot-autoconfigure引入 |
33 | dispatcherServlet SpringMVC提供的HTTP请求处理控制的集中访问点和职责派发点 由自动配置类DispatcherServletAutoConfiguration注册 |
34 | spring.mvc-org.springframework.boot.autoconfigure.web.WebMvcProperties 外部webmvc参数对应的bean定义 由自动配置类DispatcherServletAutoConfiguration 通过注解@EnableConfigurationProperties(WebMvcProperties.class)引入 |
35 | org.springframework.boot.autoconfigure.web. DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration 用于提供bean定义ServletRegistrationBean dispatcherServletRegistration 通过spring-boot-autoconfigure引入 |
36 | dispatcherServletRegistration 用于注册dispatchServlet的ServletRegistrationBean,SCI 由自动配置类DispatcherServletAutoConfiguration注册 |
37 | org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration 用于提供bean定义dispatcherServlet 和 ServletRegistrationBean dispatcherServletRegistration 通过spring-boot-autoconfigure引入 |
38 | org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration |
39 | defaultValidator |
40 | methodValidationPostProcessor |
41 | org.springframework.boot.autoconfigure.web. ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration White label error view 的配置 spring-boot-autoconfigure ErrorMvcAutoConfiguration 的嵌套@Configuration类 |
42 | error 一个显示错误的View模板,开发人员未指定时使用的缺省值 spring-boot-autoconfigure ErrorMvcAutoConfiguration嵌套White label error view配置类中定义的bean |
43 | beanNameViewResolver 从bean容器中根据bean名称找出view名称的ViewResolver,配合上面定义View bean error使用 spring-boot-autoconfigure ErrorMvcAutoConfiguration嵌套White label error view配置类中定义的bean |
44 | org.springframework.boot.autoconfigure.web. ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration 缺省错误View Resolver配置 spring-boot-autoconfigure ErrorMvcAutoConfiguration 的嵌套@Configuration类 |
45 | conventionErrorViewResolver缺省错误View Resolver配置定义的DefaultErrorViewResolver spring-boot-autoconfigure ErrorMvcAutoConfiguration 的嵌套配置类DefaultErrorViewResolverConfiguration的Bean定义 |
46 | org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration 自动配置通过一个MVC error控制器处理错误 通过spring-boot-autoconfigure引入 |
47 | errorAttributes 错误属性ErrorAttributes的缺省实现,开发人员未指定时才启用这个缺省bean spring-boot-autoconfigure ErrorMvcAutoConfiguration 的Bean定义 |
48 | basicErrorController 缺省BasicErrorController bean,基础全局错误处理Controller用于处理ErrorAttributes 开发人员未指定时才启用这个缺省baen spring-boot-autoconfigure ErrorMvcAutoConfiguration 的Bean定义 |
49 | errorPageCustomizer 向内置Servlet容器注册一个错误页面 spring-boot-autoconfigure ErrorMvcAutoConfiguration 的Bean定义 |
50 | preserveErrorControllerTargetClassPostProcessor |
51 | spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties |
52 | org.springframework.boot.autoconfigure.web. WebMvcAutoConfiguration$EnableWebMvcConfiguration |
53 | requestMappingHandlerAdapter InitializingBean,支持 @RequestMapping定义的控制器方法(HandlerMethod) |
54 | requestMappingHandlerMapping |
55 | mvcValidator |
56 | mvcContentNegotiationManager |
57 | mvcPathMatcher |
58 | mvcUrlPathHelper |
59 | viewControllerHandlerMapping |
60 | beanNameHandlerMapping |
61 | resourceHandlerMapping |
62 | mvcResourceUrlProvider |
63 | defaultServletHandlerMapping |
64 | mvcConversionService |
65 | mvcUriComponentsContributor |
66 | httpRequestHandlerAdapter |
67 | simpleControllerHandlerAdapter |
68 | handlerExceptionResolver |
69 | mvcViewResolver |
70 | mvcHandlerMappingIntrospector |
71 | org.springframework.boot.autoconfigure.web. WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter内部类FaviconConfiguration |
72 | faviconHandlerMapping |
73 | faviconRequestHandler |
74 | org.springframework.boot.autoconfigure.web. WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter WebMvcAutoConfigurationAdapter spring-boot-autoconfigure WebMvcAutoConfiguration的嵌套配置类 |
75 | defaultViewResolver |
76 | viewResolver |
77 | welcomePageHandlerMapping |
78 | requestContextFilter 1.OrderedRequestContextFilter bean 2.将request暴露给当前线程的Servlet Filter,利用LocaleContextHolder和RequestContextHolder 3.替代方案:Spring的RequestContextListener,DispatcherServlet也会将request暴露给当前线程 4.仅在未发现有其他RequestContextFilter bean或者RequestContextListener bean定义时才定义该bean spring-boot-autoconfigure WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter定义的bean |
79 | org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration Web MVC 自动配置 通过spring-boot-autoconfigure引入 |
80 | hiddenHttpMethodFilter OrderedHiddenHttpMethodFilter bean, 将post请求中实际使用的method参数转换为可以通过 HttpServletRequest.getMethod()方式访问 spring-boot-autoconfigure WebMvcAutoConfiguration的bean定义 |
81 | httpPutFormContentFilter OrderedHttpPutFormContentFilter bean, 将PUT,PATCH请求中表单参数的获取方式处理成跟POST请求中的一样:通过getParameter*()方式访问 spring-boot-autoconfigure WebMvcAutoConfiguration的bean定义 |
82 | org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration |
83 | mbeanExporter |
84 | objectNamingStrategy |
85 | mbeanServer |
86 | org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration |
87 | org.springframework.boot.autoconfigure.web. HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration |
88 | stringHttpMessageConverter |
89 | spring.http.encoding-org.springframework.boot. autoconfigure.web.HttpEncodingProperties |
90 | org.springframework.boot.autoconfigure.web. JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration |
91 | mappingJackson2HttpMessageConverter |
92 | org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration |
93 | org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration |
94 | messageConverters |
95 | org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration |
96 | spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties |
97 | org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration 配置Web应用中使用的字符编码方式 1.如果characterEncodingFilter bean不存在则增加一个 2.如果localeCharsetMappingsCustomizer bean未被定义则添加一个 3.参数前缀 spring.http.encoding,对应属性类 HttpEncodingProperties(缺省字符集是UTF-8) 通过spring-boot-autoconfigure引入 |
98 | characterEncodingFilter 1.Filter bean 2.设置理解request数据使用的字符集的Servlet Filter spring-boot-autoconfigure HttpEncodingAutoConfiguration定义的bean |
99 | localeCharsetMappingsCustomizer EmbeddedServletContainerCustomizer bean设置容器针对不同的Locale使用哪种编码方式 spring-boot-autoconfigure HttpEncodingAutoConfiguration定义的bean |
100 | org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration multi-part upload文件上传的自动配置 1.如果StandardServletMultipartResolver不存在则增加一个 2.如果MultipartConfigElement未被定义则添加一个 3.缺省情况下,EmbeddedWebApplicationContext会关联这个MultipartConfigElement 到任意一个Servlet bean用于处理文件上传 4.javax.servlet.MultipartConfigElement是用于配置容器如何处理文件上传的Servlet API 5.参数前缀 spring.http.multipart,对应属性类 MultipartProperties 通过spring-boot-autoconfigure引入 |
101 | multipartConfigElement 容器如何处理文件上传的配置 spring-boot-autoconfigure MultipartAutoConfiguration定义的bean |
102 | multipartResolver 1.一个StandardServletMultipartResolver实例bean 2.会被作为一个multipartResolver添加到一个Spring DispatcherServlet context 3.基于Servlet 3.0 javax.servlet.http.Part API spring-boot-autoconfigure MultipartAutoConfiguration定义的bean |
103 | spring.http.multipart-org.springframework.boot.autoconfigure.web.MultipartProperties |
104 | org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration 提供配置内置servlet容器ConfigurableEmbeddedServletContainer的bean ServerProperties 通过spring-boot-autoconfigure引入 |
105 | serverProperties EmbeddedServletContainerCustomizer web服务器的ConfigurationProperties,比如端口和路径设置, 被用来设置ConfigurableEmbeddedServletContainer的属性, 被应用时会向ConfigurableEmbeddedServletContainer添加两个SCI : SessionConfiguringInitializer InitParameterConfiguringServletContextInitializer 通过spring-boot-autoconfigure引入 由自动配置类ServerPropertiesAutoConfiguration注册 |
106 | duplicateServerPropertiesDetector EmbeddedServletContainerCustomizer 确保应用上下文中只有一个ServerProperties 通过spring-boot-autoconfigure引入 由自动配置类ServerPropertiesAutoConfiguration注册 |
107 | org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration$RestTemplateConfiguration |
108 | restTemplateBuilder |
109 | org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration |
相关资料
Spring ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry() 是如何工作的 ?
Spring 工具类 ConfigurationClassParser 是如何工作的 ?