SpringMVC自动配置
Spring Boot 自动配置好了springMVC
以下是springboot对springMVC的默认配置:
- Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
*自动配置好了ViewResolver(试图解析器:根据方法的返回值得到视图对象(View),
视图对象决定 如何去渲染[转发?重定向?])
*ContentNegotiatingViewResolver:组合所有的视图解析器的
*如何定制:我们可以自己给容器中添加一个视图解析器;自动将其组合进来
-
Support for serving static resources, including support for WebJars (covered later in this document)).静态资源文件夹路径webjars
-
自动注册了of Converter, GenericConverter, and Formatter beans.
Converter: 转换器 类型转换
Formatter:格式化 例如:2019-7-7===Date
自己添加的格式化转换器,我们只需要放在容器中即可
- Support for HttpMessageConverters (covered later in this document).
*HttpMessageConverter:springmvc 用来转换Http请求和响应的;
*HttpMessageConverters是从容器中确定,获取所有的HttpMessageConverter.
* 自己给容器中添加HttpMessageConverter,只需要将自己的组件注册在容器中
(@Bean,@Component)
- Automatic registration of MessageCodesResolver (covered later in this document).
- Static index.html support. 静态首页的访问
- Custom Favicon support (covered later in this document). favcion.ico
- Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).
我们可以自己配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器)
initializing all {@link WebDataBinder} instances.
请求数据==javabean
如果您想保留Spring Boot MVC功能并且想要添加其他 MVC配置(拦截器,格式化程序,视图控制器和其他功能),您可以添加自己的@Configuration类类型 WebMvcConfigurer但不需要 @EnableWebMvc。如果您希望提供,或的 自定义实例RequestMappingHandlerMapping,则可以声明 实例以提供此类组件。RequestMappingHandlerAdapter,ExceptionHandlerException,ResolverWebMvcRegistrationsAdapter
如果您想完全控制Spring MVC,可以添加自己的@Configuration 注释@EnableWebMvc。
扩展springMVC
编写一个配置类(@Configuration),实现WebMvcConfigurer接口;不能标注@EnableWebMvc
既保留了所有的自动配置,还可以使用我们自己扩展的配置
@Configuration
public class myMVConfig implements WebMvcConfigurer{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// 浏览器发送jj请求,也会到success页面
registry.addViewController("/jj").setViewName("success");
}
}
- WebMvcAutoConfiguration是springMVC的自动配置类
- 在做其他配置时会导入:@Import(EnableWebMvcConfiguration.class)
@Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
// 从容器中获取所有的WebMvcConfiguger
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
//一个参考实现
//@Override
// public void addViewControllers(ViewControllerRegistry registry) {
// for (WebMvcConfigurer delegate : this.delegates) {
// delegate.addViewControllers(registry);
// }
}
}
}
- 容器中所有的WebMvcCongurer都会一起起作用
- 我们的配置类也会被调用
效果:springMvc的自动配置和我们的自己的配置都会起作用
全面接管springMvc
Springboot对springMVC的自动配置不要了,所有的都自己配置[@EnableWebMvc]
所有的springmvc 配置都会失效
原理:
1.
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
@Configuration
@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 {
4. @EnableWebMvc将WebMvcConfigurationSupport导入了进来;
5. WebMvcConfigurationSupport只有springmvc最基本的功能
如何修改springboot的默认配置
模式:
- springboot自动配置组件的时候,先看用户有没有自己配置,(@Bean,@Component),如果没有默认自动配置才生效,如果有些组件可以有很多((eg…viewResolver),将和用户配置的和自己配置的组合起来
- 在springboot中会有非常多的xxxConfigurer帮助我们进行扩展配置