缺省配置Springboot Web应用启动过程中定义的Bean

103 篇文章 18 订阅
65 篇文章 6 订阅

1. ApplicationContext 对象构建时登记 6 个 bean 定义

登记时机

AnnotationConfigEmbeddedWebApplicationContext.AnnotationConfigEmbeddedWebApplicationContext
    -> new AnnotatedBeanDefinitionReader(this)
    -> AnnotatedBeanDefinitionReader.AnnotatedBeanDefinitionReader(registry,environment)
    -> AnnotationConfigUtils.registerAnnotationConfigProcessors(registry)    
  1. 对于完全采用缺省配置的Springboot Web应用,其ApplicationContext的实现类最终是
    AnnotationConfigEmbeddedWebApplicationContext,该类位于包 org.springframework.boot.context.embedded
  2. 该类AnnotationConfigEmbeddedWebApplicationContext在构造函数中会创建一个AnnotatedBeanDefinitionReader对象用于读取注解方式的Bean定义。
  3. 而这个类AnnotatedBeanDefinitionReader在其构造函数中,又进一步调用AnnotationConfigUtils.registerAnnotationConfigProcessors(registry) 一次性登记了6个Bean定义。

6个bean定义的名称

类名Bean名称(共同前缀:org.springframework.context.)
ConfigurationClassPostProcessorannotation.internalConfigurationAnnotationProcessor
AutowiredAnnotationBeanPostProcessorannotation.internalAutowiredAnnotationProcessor
RequiredAnnotationBeanPostProcessorannotation.internalRequiredAnnotationProcessor
CommonAnnotationBeanPostProcessorannotation.internalCommonAnnotationProcessor
EventListenerMethodProcessorevent.internalEventListenerProcessor
DefaultEventListenerFactoryevent.internalEventListenerFactory

6个bean定义的作用

类名类型作用简介
ConfigurationClassPostProcessorBeanPostProcessor
BeanDefinitionRegistryPostProcessor
应用启动过程中对@Configuration配置类的处理
AutowiredAnnotationBeanPostProcessorBeanPostProcessor
SmartInstantiationAwareBeanPostProcessor
MergedBeanDefinitionPostProcessor
PriorityOrdered
自动装配(autowire)被注解的属性,set方法或其他任意的配置方法。支持的注解 : @Autowared, @Value, JSR-330 @Inject
RequiredAnnotationBeanPostProcessorBeanPostProcessor
SmartInstantiationAwareBeanPostProcessor
MergedBeanDefinitionPostProcessor
PriorityOrdered
处理注解 @Required
CommonAnnotationBeanPostProcessorBeanPostProcessor
InstantiationAwareBeanPostProcessor
DestructionAwareBeanPostProcessor
MergedBeanDefinitionPostProcessor
PriorityOrdered
支持通用Java注解,尤其是JSR-250注解,也就是javax.annotation包内的那些注解。比如 @PostConstruct, @PreDestroy,@Resource@WebServiceRef
EventListenerMethodProcessorSmartInitializingSingleton@EventListener注解的方法作为单个ApplicationListener实例注册
DefaultEventListenerFactoryEventListenerFactory
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名称
SharedMetadataReaderFactoryBeanorg.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名称
0org.springframework.context.annotation.internalConfigurationAnnotationProcessor
1org.springframework.context.annotation.internalAutowiredAnnotationProcessor
2org.springframework.context.annotation.internalRequiredAnnotationProcessor
3org.springframework.context.annotation.internalCommonAnnotationProcessor
4org.springframework.context.event.internalEventListenerProcessor
5org.springframework.context.event.internalEventListenerFactory
6application
开发人员自己的入口主程序
7org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
8sampleController
开发人员自己提供的Web控制器类
9org.springframework.boot.autoconfigure.AutoConfigurationPackages
开发人员自己的入口主程序上隐含了注解@AutoConfigurationPackage
导致入口主程序所在包被注册为该bean
10org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
11org.springframework.boot.autoconfigure.condition.BeanTypeRegistry
12propertySourcesPlaceholderConfigurer
13org.springframework.boot.autoconfigure.jackson.
JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration
14standardJacksonObjectMapperBuilderCustomizer
15spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties
16org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
17org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.store
18org.springframework.boot.autoconfigure.jackson.
JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration
19jacksonObjectMapperBuilder
20org.springframework.boot.autoconfigure.jackson.
JacksonAutoConfiguration$JacksonObjectMapperConfiguration
21jacksonObjectMapper
22org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
23jsonComponentModule
24org.springframework.boot.autoconfigure.websocket.
WebSocketAutoConfiguration$TomcatWebSocketConfiguration
25websocketContainerCustomizer
26org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration
27org.springframework.boot.autoconfigure.web.
EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat
嵌入式tomcat servlet容器的工厂配置类(@Configuration)
通过spring-boot-autoconfigure引入
因为spring-boot-starter-tomcat的存在被应用
28tomcatEmbeddedServletContainerFactory
嵌入式tomcat servlet容器的工厂
通过spring-boot-autoconfigure引入
因为spring-boot-starter-tomcat的存在被应用
29org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
嵌入式servlet容器的自动配置类(@Configuration)
通过spring-boot-autoconfigure引入
根据实际依赖于哪个servlet容器实现包决定应用哪个servlet容器实现类
30embeddedServletContainerCustomizerBeanPostProcessor
BeanPostProcessor,用来对ConfigurableEmbeddedServletContainer应用
所有bean容器中注册的EmbeddedServletContainerCustomizer

由自动配置类EmbeddedServletContainerAutoConfiguration注册
31errorPageRegistrarBeanPostProcessor

由自动配置类EmbeddedServletContainerAutoConfiguration注册
32org.springframework.boot.autoconfigure.web.
DispatcherServletAutoConfiguration$DispatcherServletConfiguration
用于提供dispatcherServlet bean定义
通过spring-boot-autoconfigure引入
33dispatcherServlet
SpringMVC提供的HTTP请求处理控制的集中访问点和职责派发点
由自动配置类DispatcherServletAutoConfiguration注册
34spring.mvc-org.springframework.boot.autoconfigure.web.WebMvcProperties
外部webmvc参数对应的bean定义
由自动配置类DispatcherServletAutoConfiguration
通过注解@EnableConfigurationProperties(WebMvcProperties.class)引入
35org.springframework.boot.autoconfigure.web.
DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration
用于提供bean定义ServletRegistrationBean dispatcherServletRegistration
通过spring-boot-autoconfigure引入
36dispatcherServletRegistration
用于注册dispatchServlet的ServletRegistrationBean,SCI
由自动配置类DispatcherServletAutoConfiguration注册
37org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration
用于提供bean定义dispatcherServlet 和 ServletRegistrationBean dispatcherServletRegistration
通过spring-boot-autoconfigure引入
38org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration
39defaultValidator
40methodValidationPostProcessor
41org.springframework.boot.autoconfigure.web.
ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration
White label error view 的配置
spring-boot-autoconfigure ErrorMvcAutoConfiguration 的嵌套@Configuration类
42error
一个显示错误的View模板,开发人员未指定时使用的缺省值
spring-boot-autoconfigure ErrorMvcAutoConfiguration嵌套White label error view配置类中定义的bean
43beanNameViewResolver
从bean容器中根据bean名称找出view名称的ViewResolver,配合上面定义View bean error使用
spring-boot-autoconfigure ErrorMvcAutoConfiguration嵌套White label error view配置类中定义的bean
44org.springframework.boot.autoconfigure.web.
ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration
缺省错误View Resolver配置
spring-boot-autoconfigure ErrorMvcAutoConfiguration 的嵌套@Configuration类
45conventionErrorViewResolver缺省错误View Resolver配置定义的DefaultErrorViewResolver
spring-boot-autoconfigure ErrorMvcAutoConfiguration 的嵌套配置类DefaultErrorViewResolverConfiguration的Bean定义
46org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
自动配置通过一个MVC error控制器处理错误
通过spring-boot-autoconfigure引入
47errorAttributes
错误属性ErrorAttributes的缺省实现,开发人员未指定时才启用这个缺省bean
spring-boot-autoconfigure ErrorMvcAutoConfiguration 的Bean定义
48basicErrorController
缺省BasicErrorController bean,基础全局错误处理Controller用于处理ErrorAttributes
开发人员未指定时才启用这个缺省baen
spring-boot-autoconfigure ErrorMvcAutoConfiguration 的Bean定义
49errorPageCustomizer
向内置Servlet容器注册一个错误页面
spring-boot-autoconfigure ErrorMvcAutoConfiguration 的Bean定义
50preserveErrorControllerTargetClassPostProcessor
51spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties
52org.springframework.boot.autoconfigure.web.
WebMvcAutoConfiguration$EnableWebMvcConfiguration
53requestMappingHandlerAdapter
InitializingBean,支持 @RequestMapping定义的控制器方法(HandlerMethod)
54requestMappingHandlerMapping
55mvcValidator
56mvcContentNegotiationManager
57mvcPathMatcher
58mvcUrlPathHelper
59viewControllerHandlerMapping
60beanNameHandlerMapping
61resourceHandlerMapping
62mvcResourceUrlProvider
63defaultServletHandlerMapping
64mvcConversionService
65mvcUriComponentsContributor
66httpRequestHandlerAdapter
67simpleControllerHandlerAdapter
68handlerExceptionResolver
69mvcViewResolver
70mvcHandlerMappingIntrospector
71org.springframework.boot.autoconfigure.web.
WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter内部类FaviconConfiguration
72faviconHandlerMapping
73faviconRequestHandler
74org.springframework.boot.autoconfigure.web.
WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
WebMvcAutoConfigurationAdapter
spring-boot-autoconfigure WebMvcAutoConfiguration的嵌套配置类
75defaultViewResolver
76viewResolver
77welcomePageHandlerMapping
78requestContextFilter
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
79org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
Web MVC 自动配置
通过spring-boot-autoconfigure引入
80hiddenHttpMethodFilter
OrderedHiddenHttpMethodFilter bean,
将post请求中实际使用的method参数转换为可以通过 HttpServletRequest.getMethod()方式访问
spring-boot-autoconfigure WebMvcAutoConfiguration的bean定义
81httpPutFormContentFilter
OrderedHttpPutFormContentFilter bean,
将PUT,PATCH请求中表单参数的获取方式处理成跟POST请求中的一样:通过getParameter*()方式访问
spring-boot-autoconfigure WebMvcAutoConfiguration的bean定义
82org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
83mbeanExporter
84objectNamingStrategy
85mbeanServer
86org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
87org.springframework.boot.autoconfigure.web.
HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration
88stringHttpMessageConverter
89spring.http.encoding-org.springframework.boot.
autoconfigure.web.HttpEncodingProperties
90org.springframework.boot.autoconfigure.web.
JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration
91mappingJackson2HttpMessageConverter
92org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration
93org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration
94messageConverters
95org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
96spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
97org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration
配置Web应用中使用的字符编码方式
1.如果characterEncodingFilter bean不存在则增加一个
2.如果localeCharsetMappingsCustomizer bean未被定义则添加一个
3.参数前缀 spring.http.encoding,对应属性类 HttpEncodingProperties(缺省字符集是UTF-8)

通过spring-boot-autoconfigure引入
98characterEncodingFilter
1.Filter bean
2.设置理解request数据使用的字符集的Servlet Filter
spring-boot-autoconfigure HttpEncodingAutoConfiguration定义的bean
99localeCharsetMappingsCustomizer
EmbeddedServletContainerCustomizer bean设置容器针对不同的Locale使用哪种编码方式
spring-boot-autoconfigure HttpEncodingAutoConfiguration定义的bean
100org.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引入
101multipartConfigElement
容器如何处理文件上传的配置
spring-boot-autoconfigure MultipartAutoConfiguration定义的bean
102multipartResolver
1.一个StandardServletMultipartResolver实例bean
2.会被作为一个multipartResolver添加到一个Spring DispatcherServlet context
3.基于Servlet 3.0 javax.servlet.http.Part API
spring-boot-autoconfigure MultipartAutoConfiguration定义的bean
103spring.http.multipart-org.springframework.boot.autoconfigure.web.MultipartProperties
104org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
提供配置内置servlet容器ConfigurableEmbeddedServletContainer的bean ServerProperties
通过spring-boot-autoconfigure引入
105serverProperties
EmbeddedServletContainerCustomizer
web服务器的ConfigurationProperties,比如端口和路径设置,
被用来设置ConfigurableEmbeddedServletContainer的属性,
被应用时会向ConfigurableEmbeddedServletContainer添加两个SCI :
SessionConfiguringInitializer
InitParameterConfiguringServletContextInitializer

通过spring-boot-autoconfigure引入
由自动配置类ServerPropertiesAutoConfiguration注册
106duplicateServerPropertiesDetector
EmbeddedServletContainerCustomizer
确保应用上下文中只有一个ServerProperties

通过spring-boot-autoconfigure引入
由自动配置类ServerPropertiesAutoConfiguration注册
107org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration$RestTemplateConfiguration
108restTemplateBuilder
109org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration

相关资料

Spring ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry() 是如何工作的 ?

Spring 工具类 ConfigurationClassParser 是如何工作的 ?

Spring boot web 应用 Bean介绍 : AutoConfigurationPackages

SpringApplication 的启动过程的两个阶段

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值