Inclusion of ContentNegotiatingViewResolver
and BeanNameViewResolver
beans.
ContentNegotiatingViewResolver
组合所有的视图解析器Support for serving static resources, including support for WebJars (covered later in this document)).
Automatic registration of Converter
, GenericConverter
, and Formatter
beans.
自动注册了
Converter
类型转换器 :String ===> 封装进去User
Formatter
格式化器 :2017-12-17 ===> Date
public void addFormatters(FormatterRegistry registry) {
ApplicationConversionService.addBeans(registry, this.beanFactory);
}
public static void addBeans(FormatterRegistry registry, ListableBeanFactory beanFactory) {
Set<Object> beans = new LinkedHashSet();
beans.addAll(beanFactory.getBeansOfType(GenericConverter.class).values());
beans.addAll(beanFactory.getBeansOfType(Converter.class).values());
所以springboot同时也将容器中所有的Converter
Formatter
GenericConverter
加入到组件中
自己添加的格式化转换器,我们只需要放在容器中即可
Support for HttpMessageConverters
(covered later in this document).
HttpMessageConverters : SpringMVC用来转换Http请求和相应的;(User对象转换成Json数据)
只有一个构造器给ObjectProvider赋值 ,所以参数直接从容器中获取, HttpMessageConverters 是从容器中确定,获取所有的HttpMessageConverter
private final ObjectProvider<HttpMessageConverters> messageConvertersProvider;
public WebMvcAutoConfigurationAdapter(…… ObjectProvider<HttpMessageConverters> messageConvertersProvider…………) {
this.messageConvertersProvider = messageConvertersProvider;
自己添加的HttpMessageConverter,只需要直接放入容器(@Bean、@Component)
Automatic registration of MessageCodesResolver
(covered later in this document).
Static index.html
support.
Custom Favicon
support (covered later in this document).
Automatic use of a ConfigurableWebBindingInitializer
bean (covered later in this document).
我们可以配置一个ConfigurableWebBindingInitializer
来替换默认的;(添加到容器)
初始化WebDataBinder
请求数据===JavaBean 将数据绑定成为我们要封装的对象,用到类型转换器,格式化器等等
ConfigurableWebBindingInitializer
public void initBinder(WebDataBinder binder) {
binder.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
if (this.directFieldAccess) {
binder.initDirectFieldAccess();
}
org.springframework.boot.autoconfigure.web:web的所有自动场景;
If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration
class of type WebMvcConfigurer
but without @EnableWebMvc
.
<mvc:view-controller path="/hello" view-name="success"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
编写一个配置类(Configuration),是WebMvcConfigurer
类型,不能标注@EnableWebMvc
@Configuration
//WebMvcConfigurer接口中有很多方法的对应的是以往Springmvc配置文件中的属性
//使用其来扩展SpringMVC的功能
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/jiat请求来到 success
registry.addViewController("/jiat").setViewName("success");
}
}
原理:
SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置,所有的SpringMVC模块自动配置全部失效,包括静态资源
@EnableWebMvc能使默认配置生效的原因
添加了@EnableWebMvc后会为容器导入一个WebMvcConfigurationSupport配置类,添加相应组件到容器中
@Import({DelegatingWebMvcConfiguration.class}) //导入一个DelegatingWebMvcConfiguration
public @interface EnableWebMvc {
//为容器添加一个WebMvcConfigurationSupport
@Configuration(
proxyBeanMethods = false
)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
而 WebMvcAutoConfiguration 的注解上有一个“@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})”,当存在如果容器中存在WebMvcConfigurationSupport对象时,WebMvcAutoConfiguration 自动配置失效
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
//如果容器中存在WebMvcConfigurationSupport对象,则以下所有配置不生效
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
接管SpringMVC总结:
模式:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章