1. Web场景
SpringBoot的Web开发能力,由SpringMVC提供。
1. 自动配置
-
整合web场景
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
引入了
autoconfigure
自动配置功能。 -
@EnableAutoConfiguration
注解使用@Import(AutoConfigurationImportSelector.class)
批量导入组件 -
加载
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件中配置的所有组件 -
所有自动配置类如下
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration ====以下是响应式web场景和现在的没关系====== org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration org.springframework.boot.autoconfigure.web.reactive.ReactiveMultipartAutoConfiguration org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration org.springframework.boot.autoconfigure.web.reactive.WebSessionIdResolverAutoConfiguration org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration ================以上没关系================= org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
-
绑定了配置文件的一堆配置项
- SpringMVC的所有配置
spring.mvc
- Web场景通用配置
spring.web
- 文件上传配置
spring.servlet.multipart
- 服务器的配置
server
: 比如:编码方式
- SpringMVC的所有配置
2. 默认效果
默认配置:
- 包含了
ContentNegotiatingViewResolver
和BeanNameViewResolver 组件
,方便视图解析 - 默认的静态资源处理机制: 静态资源放在 static 文件夹下即可直接访问
- 自动注册了 Converter,GenericConverter,Formatter组件,适配常见数据类型转换和格式化需求
- 支持 HttpMessageConverters,可以方便返回
json
等数据类型 - 注册 MessageCodesResolver,方便国际化及错误消息处理
- 支持 静态
index.html
- 自动使用
ConfigurableWebBindingInitializer
,实现消息处理、数据绑定、类型转化、数据校验等功能
重要:
- 如果想保持 boot mvc 的默认配置,并且自定义更多的 mvc 配置,如:interceptors, formatters, view controllers 等。可以使用
@Configuration
注解添加一个WebMvcConfigurer
类型的配置类,并不要标注@EnableWebMvc
- 如果想保持 boot mvc 的默认配置,但要自定义核心组件实例,比如:RequestMappingHandlerMapping, RequestMappingHandlerAdapter, 或ExceptionHandlerExceptionResolver,给容器中放一个
WebMvcRegistrations
组件即可- 如果想全面接管 Spring MVC,
@Configuration
标注一个配置类,并加上 @EnableWebMvc注解,实现
WebMvcConfigurer 接口`
三种方式
方式 | 用法 | 效果 | |
---|---|---|---|
全自动 | 直接编写控制器逻辑 | 全部使用自动配置默认效果 | |
手自一体 | @Configuration + 配置 WebMvcConfigurer +_配置 WebMvcRegistrations _ | 不要标注@EnableWebMvc | 保留自动配置效果 手动设置部分功能 定义MVC底层组件 |
全手动 | @Configuration + 配置 WebMvcConfigurer | 标注@EnableWebMvc | 禁用自动配置效果 全手动设置 |
总结:
给容器中写一个配置类@Configuration
实现 WebMvcConfigurer
但是不要标注 @EnableWebMvc
注解,实现手自一体的效果。
两种模式
1、前后分离模式
: @RestController
响应JSON数据
2、前后不分离模式
:@Controller + Thymeleaf模板引擎