官方文档
29.1.1 Spring MVC Auto-configuration
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.The auto-configuration adds the following features on top of Spring’s defaults:
-
Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
1、自动配置ViewResolver(视图解析器:根据方法的返回值获得视图对象(View),视图对象决定如何渲染(转发?重定向?)) 2、ContentNegotiatingViewResolver组合了所有的视图解析器 3、如何定制?我们只需要给容器添加一个视图解析器,自动回将其组合进来
-
Support for serving static resources, including support for WebJars (covered later in this document)).
-
Automatic registration of Converter, GenericConverter, and Formatter beans.
1、Converter:转换器,比如public String test(User user);表单中传入的数字,true/false等都是文本,这些文本需要映射到User中,需要转换器,进行类型转换,将text(数字)-->int/integer,text(true)-->bool 2、Formatter :格式化,2017/0/101--->Date
-
Support for Http MessageConverters(covered later in this document ).
1、HttpMessageConverters:是SpringMVC用来http请求和响应的,比如将User--->json返回 2、HttpMessageConverters是从容器中确定的
-
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).
1、扩展SpringMvc
如果想要在Spring Boot中使用mvc中的一些扩展功能,我们要编写一个配置类@configuration,继承WebMvcConfigurerAdapter,这样就可以既保留了所有的自动配置,也能用我们扩展的配置。
注意:不能标准@EnableWebMvc注解
快捷方式:Ctrl+O
再继承或者实现了父类,想要快速知道父类里面实现的方法,可以使用Ctrl+O进行查看。例如:
实现:
package com.example.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//super.addViewControllers(registry);
//浏览器发送/heMvc就可以成功访问到success界面
registry.addViewController("/heMvc").setViewName("success");
}
}
success.html
<h1>使用Mvc渲染</h1>
访问http://localhost:8080/heMvc结果:
原理:
- WebMvcAutoConfiguration是SpringMVC的自动配置类;
- 在做其他自动配置时会导入@Import(EnableWebMvcConfiguration.class)
- 容器中的所有WebMvcConfigurer都会一起起作用
- 我们的配置类也会被调用
2、全面接管SpringMVC
Springboot对所有SpringMVC的自动配置都不需要了,使所有自动配置都失效。我们只要在配置类中加上@EnableWebMvc即可。
为什么加上@EnableWebMvc注解后spring mvc就是失效了呢?
- @EnableWebMvc的核心
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
- 查看DelegatingWebMvcConfiguration类
@Configuration(
proxyBeanMethods = false
)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
- 查看自动配置的WebMvcAutoConfiguration 类,发现容器中没有WebMvcConfigurationSupport这个组件才生效
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
//查看容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
- @EnableWebMvc将WebMvcConfigurationSupport这个组件导入进来
- 导入WebMvcConfigurationSupport只是SpringMVC最基本的功能
3、如何修改SpringBoot的默认配置
模式:
1、SpringBoot在自动配置的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;
2、在SpringBoot中会有非常多的xxxConfigurer邦族我们进行扩展配置;