查看“Spring Boot 自动配置特性”的源代码
←
Spring Boot 自动配置特性
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV19K4y1L7MT/?p=7 === 怎么自动配好 Tomcat的? === ==== 怎么引入 tomcat 依赖的? ==== boot-01-helloworld - pom.xml<syntaxhighlight lang="xml"> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </syntaxhighlight> spring-boot-starter-web-x.x.x.RELEASE.pom<syntaxhighlight lang="xml"> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.3.4.RELEASE</version> <scope>compile</scope> </dependency> </syntaxhighlight> === 自动配置好 Spring MVC === spring-boot-starter-web-x.x.x.RELEASE.pom<syntaxhighlight lang="xml"> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.9.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.9.RELEASE</version> <scope>compile</scope> </dependency> </syntaxhighlight> === 思考:在 Spring Boot 出现之前,使用 Spring MVC 要有哪些配置? === web.xml<syntaxhighlight lang="xml"> <!--配置DispatcherServlet --> <servlet> <servlet-name>spring-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置SpringMVC需要加载的配置文件 spring-mvc.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <!--默认匹配所有的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> </syntaxhighlight>为什么在 Spring Boot 出现之后,不进行如上的配置也可以进行使用;如果这些配置已经在 Spring Boot 内部实现了,是怎么实现的? ==== 打印容器中所有的 bean ==== <syntaxhighlight lang="java"> package io.github.jihch.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; /** * 主程序类 * @SpringBootApplication:这是一个 Spring Boot 应用 */ @SpringBootApplication public class MainApplication { public static void main(String[] args) { //1、返回 IOC 容器 ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args); //2、查看容器里面的组件 String[] beanDefinitionNames = context.getBeanDefinitionNames(); Arrays.sort(beanDefinitionNames); for (String name : beanDefinitionNames) { System.out.println(name); } } } </syntaxhighlight><syntaxhighlight lang="console"> applicationAvailability applicationTaskExecutor basicErrorController beanNameHandlerMapping beanNameViewResolver characterEncodingFilter conventionErrorViewResolver defaultServletHandlerMapping defaultViewResolver dispatcherServlet dispatcherServletRegistration error errorAttributes errorPageCustomizer errorPageRegistrarBeanPostProcessor formContentFilter handlerExceptionResolver handlerFunctionAdapter helloController httpRequestHandlerAdapter jacksonObjectMapper jacksonObjectMapperBuilder jsonComponentModule lifecycleProcessor localeCharsetMappingsCustomizer mainApplication mappingJackson2HttpMessageConverter messageConverters multipartConfigElement multipartResolver mvcContentNegotiationManager mvcConversionService mvcHandlerMappingIntrospector mvcPathMatcher mvcResourceUrlProvider mvcUriComponentsContributor mvcUrlPathHelper mvcValidator mvcViewResolver org.springframework.aop.config.internalAutoProxyCreator org.springframework.boot.autoconfigure.AutoConfigurationPackages org.springframework.boot.autoconfigure.aop.AopAutoConfiguration org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$ParameterNamesModuleConfiguration org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration org.springframework.boot.context.internalConfigurationPropertiesBinder org.springframework.boot.context.internalConfigurationPropertiesBinderFactory org.springframework.boot.context.properties.BoundConfigurationProperties org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.event.internalEventListenerFactory org.springframework.context.event.internalEventListenerProcessor parameterNamesModule preserveErrorControllerTargetClassPostProcessor propertySourcesPlaceholderConfigurer requestContextFilter requestMappingHandlerAdapter requestMappingHandlerMapping resourceHandlerMapping restTemplateBuilder routerFunctionMapping server-org.springframework.boot.autoconfigure.web.ServerProperties servletWebServerFactoryCustomizer simpleControllerHandlerAdapter spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties spring.lifecycle-org.springframework.boot.autoconfigure.context.LifecycleProperties spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties standardJacksonObjectMapperBuilderCustomizer stringHttpMessageConverter taskExecutorBuilder taskSchedulerBuilder tomcatServletWebServerFactory tomcatServletWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer viewControllerHandlerMapping viewResolver webServerFactoryCustomizerBeanPostProcessor websocketServletWebServerCustomizer welcomePageHandlerMapping </syntaxhighlight>我们可以发现,已经有了: * dispatcherServlet 前端转发控制器 * characterEncodingFilter 字符集编码过滤器 * 各种解析器 **viewResolver 视图解析器 **beanNameViewResolver **defaultViewResolver **conventionErrorViewResolver **mvcViewResolver **multipartResolver 文件上传解析器 * helloController 用 @RestController 注解的类的实例 bean === 默认的包扫描规则/如何组织包结构 === https://docs.spring.io/spring-boot/docs/2.4.13/reference/html/using-spring-boot.html#using-boot-structuring-your-code ==== 默认的包结构 ==== * 主程序所在包及其下面的所有子包里面的所有组件都会被默认扫描并初始化为容器中的 bean。所以 io.github.jihch.boot.MainApplication 运行之后可以直接找到 @RestController 注解的类 io.github.jihch.boot.controller.HelloController 的实例 bean。但如果类被定义在了上层包中,比如:<syntaxhighlight lang="java"> package io.github.jihch; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WorldController { @RequestMapping("/w") public String world66() { return "World"; } } </syntaxhighlight>容器中就找不到对应的 bean,URL 访问也是 404。如果就是要放在上层路径中,那么可以通过<syntaxhighlight lang="java"> @SpringBootApplication(scanBasePackages = "io.github.jihch") </syntaxhighlight>中的 <code>scanBasePackages</code> 来指定要扫描的包。 :还可以在同级或子级包的配置类(使用了 <code>@Configuration</code> 注解的类)中,使用 <code>@ComponentScan("io.github.jihch")</code> 来指定要扫描的包 ===各种自动配置都拥有默认值=== https://docs.spring.io/spring-boot/docs/2.7.8/reference/html/application-properties.html#appendix.application-properties 这些默认配置的值最终都是绑定到 Java 类的,这些类会在容器中创建对象 pom.xml 中增加:<syntaxhighlight lang="xml"> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </syntaxhighlight> 依赖,可以实现点击配置文件的属性跳转到对应的配置类 ===引入了哪些场景 starter,那些 starter 的自动配置才会开启 === 所有的 starter 都会依赖 spring-boot-starter,spring-boot-starter 又依赖了 spring-boot-autoconfigure,yml 中的所有自动配置项对应绑定到的 XXXProperties 类都在 spring-boot-autoconfigure 这个包里面, 而这个包里面的 XXXAutoConfiguration 都使用了 <code>@ConditionalOnClass</code> 进行条件判断,因为各种各样的 starter 会把类加载到类路径中,容器启动时扫描类路径就知道哪些 <code>@ConditionalOnClass</code> 是成立的, 然后就会再实例化对应的配置属性类 XXXProperties、绑定 yml 中的参数到这个类的对象,如果没有对应参数就会使用默认参数配置。 Spring Boot 所有的自动配置功能都在<syntaxhighlight lang="xml"> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </syntaxhighlight>spring-boot-autoconfigure-x.x.x.RELEASE.jar 里
返回至
Spring Boot 自动配置特性
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息