springboot 自动配置了哪些东西?

  • Tomcat
  • SpringMVC
  • 默认的包结构
  • 自动配置好 Web 的常见功能,如:字符编码问题
  • 各种配置的默认值
  • 按需加载所有自动配置项

查看 springboot 为我们配置了什么组件

将 main 方法的

  1. SpringApplication.run(MainApplication.class, args);

修改为:

  1. // 返回 IOC 容器
  2. ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
  3. // 查看容器里的组件
  4. String[] names = run.getBeanDefinitionNames();
  5. for (String name : names) {
  6. System.out.println(name);
  7. }

输出:

  1. org.springframework.context.annotation.internalConfigurationAnnotationProcessor
  2. org.springframework.context.annotation.internalAutowiredAnnotationProcessor
  3. org.springframework.context.annotation.internalCommonAnnotationProcessor
  4. org.springframework.context.event.internalEventListenerProcessor
  5. org.springframework.context.event.internalEventListenerFactory
  6. mainApplication
  7. org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
  8. helloController
  9. org.springframework.boot.autoconfigure.AutoConfigurationPackages
  10. org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
  11. propertySourcesPlaceholderConfigurer
  12. org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration
  13. websocketServletWebServerCustomizer
  14. org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration
  15. org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat
  16. tomcatServletWebServerFactory
  17. org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
  18. servletWebServerFactoryCustomizer
  19. tomcatServletWebServerFactoryCustomizer
  20. org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
  21. org.springframework.boot.context.internalConfigurationPropertiesBinderFactory
  22. org.springframework.boot.context.internalConfigurationPropertiesBinder
  23. org.springframework.boot.context.properties.BoundConfigurationProperties
  24. org.springframework.boot.context.properties.EnableConfigurationPropertiesRegistrar.methodValidationExcludeFilter
  25. server-org.springframework.boot.autoconfigure.web.ServerProperties
  26. webServerFactoryCustomizerBeanPostProcessor
  27. errorPageRegistrarBeanPostProcessor
  28. org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration
  29. dispatcherServlet
  30. spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties
  31. org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration
  32. dispatcherServletRegistration
  33. org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
  34. org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration
  35. taskExecutorBuilder
  36. applicationTaskExecutor
  37. spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties
  38. org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration
  39. error
  40. beanNameViewResolver
  41. org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration
  42. conventionErrorViewResolver
  43. spring.web-org.springframework.boot.autoconfigure.web.WebProperties
  44. spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties
  45. org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
  46. errorAttributes
  47. basicErrorController
  48. errorPageCustomizer
  49. preserveErrorControllerTargetClassPostProcessor
  50. org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
  51. requestMappingHandlerAdapter
  52. requestMappingHandlerMapping
  53. welcomePageHandlerMapping
  54. localeResolver
  55. themeResolver
  56. flashMapManager
  57. mvcConversionService
  58. mvcValidator
  59. mvcContentNegotiationManager
  60. mvcPatternParser
  61. mvcUrlPathHelper
  62. mvcPathMatcher
  63. viewControllerHandlerMapping
  64. beanNameHandlerMapping
  65. routerFunctionMapping
  66. resourceHandlerMapping
  67. mvcResourceUrlProvider
  68. defaultServletHandlerMapping
  69. handlerFunctionAdapter
  70. mvcUriComponentsContributor
  71. httpRequestHandlerAdapter
  72. simpleControllerHandlerAdapter
  73. handlerExceptionResolver
  74. mvcViewResolver
  75. mvcHandlerMappingIntrospector
  76. viewNameTranslator
  77. org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
  78. defaultViewResolver
  79. viewResolver
  80. requestContextFilter
  81. org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
  82. formContentFilter
  83. org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
  84. mbeanExporter
  85. objectNamingStrategy
  86. mbeanServer
  87. org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration
  88. springApplicationAdminRegistrar
  89. org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration
  90. org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
  91. org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration
  92. applicationAvailability
  93. org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
  94. org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration
  95. lifecycleProcessor
  96. spring.lifecycle-org.springframework.boot.autoconfigure.context.LifecycleProperties
  97. org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration
  98. standardJacksonObjectMapperBuilderCustomizer
  99. spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties
  100. org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration
  101. jacksonObjectMapperBuilder
  102. org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$ParameterNamesModuleConfiguration
  103. parameterNamesModule
  104. org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration
  105. jacksonObjectMapper
  106. org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
  107. jsonComponentModule
  108. org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration
  109. stringHttpMessageConverter
  110. org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration
  111. mappingJackson2HttpMessageConverter
  112. org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration
  113. org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration
  114. messageConverters
  115. org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
  116. spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
  117. org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration
  118. taskSchedulerBuilder
  119. spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
  120. org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
  121. restTemplateBuilderConfigurer
  122. restTemplateBuilder
  123. org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration
  124. tomcatWebServerFactoryCustomizer
  125. org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
  126. org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration
  127. characterEncodingFilter
  128. localeCharsetMappingsCustomizer
  129. org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration
  130. multipartConfigElement
  131. multipartResolver
  132. spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties
  133. org.springframework.aop.config.internalAutoProxyCreator

里面包含了大多数 web 开发的常见组件。

默认的包结构

文档:https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-using-the-default-package

主程序所在的包及其下面所有的子包里面的组件都会被默认扫描到,无需配置以前的包扫描。

springboot 也推荐我们将主应用程序类放在其他类之上的根包中。

目录结构:

  1. com
  2. +- example
  3. +- myapplication
  4. +- Application.java
  5. |
  6. +- customer
  7. | +- Customer.java
  8. | +- CustomerController.java
  9. | +- CustomerService.java
  10. | +- CustomerRepository.java
  11. |
  12. +- order
  13. +- Order.java
  14. +- OrderController.java
  15. +- OrderService.java
  16. +- OrderRepository.java

如果一定要放到其他包下面去,可以使用:

扩大包扫描范围

配置主程序的 @SpringBootApplication 注解,为其加上 scanBasePackages 属性,扩大包的扫描范围即可。

  1. @SpringBootApplication(scanBasePackages = "com.jaded")

各种配置的默认值

image.png
默认配置都是映射到某一个类上面的。
配置文件的值最终会绑定到每个类上,这个类会在容器中创建对象。

按需加载自动配置项

springboot 所有的自动配置功能都在 spring-boot-autoconfigure 包里面

附录

IOC是什么:https://blog.csdn.net/ivan820819/article/details/79744797