spring 的WebMvc 配置

本文探讨了为何在Spring Boot中继承WebMvcConfigurationSupport后部分配置失效的问题,以及WebMvcConfigurer接口的作用。WebMvcConfigurationSupport通过DelegatingWebMvcConfiguration实现自定义配置,但使用@EnableWebMvc会覆盖默认配置。WebMvcConfigurer是Spring MVC的配置接口,用于Java配置替代XML。重点介绍了其主要方法如addInterceptors、configureContentNegotiation等,并解释了@EnableWebMvc和@EnableAutoConfiguration的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WebMvcConfigurationSupport、WebMvcConfigurer、@EnableWebMvc

问题:为什么继承了WebMvcConfigurationSupport后有些配置会不生效呢?WebMvcConfigurer又是什么呢?

简介:

SpringBoot帮我们做了很多的事情,但是有的时候会有自定义的Handler,Interceptor,ViewResolver,MessageConverter等,该怎么配置呢?为什么继承了WebMvcConfigurationSupport后有些配置会不生效呢?WebMvcConfigurer又是什么呢?我们继承WebMvcConfigurationSupport可以自定义SpringMvc的配置。

SpringBoot帮我们做了很多的事情,但是有的时候会有自定义的Handler,Interceptor,ViewResolver,MessageConverter等,该怎么配置呢?为什么继承了WebMvcConfigurationSupport后有些配置会不生效呢?WebMvcConfigurer又是什么呢?

1、WebMvcConfigurationSupport

我们继承WebMvcConfigurationSupport可以自定义SpringMvc的配置。

跟踪发现DelegatingWebMvcConfiguration类是WebMvcConfigurationSupport的一个实现类,DelegatingWebMvcConfiguration类的setConfigurers方法可以收集所有的WebMvcConfigurer实现类中的配置组合起来,组成一个超级配置(这些配置会覆盖掉默认的配置)。而@EnableWebMvc又引入了DelegatingWebMvcConfiguration。

什么是DelegatingWebMvcConfiguration?

DelegatingWebMvcConfiguration是对Spring MVC进行配置的一个代理类。它结合缺省配置和用户配置定义Spring MVC运行时最终使用的配置。

	private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();


	// 注入一组WebMvcConfigurer,这些WebMvcConfigurer由开发人员提供,或者框架其他部分提供,已经
	// 以bean的方式注入到容器中
	@Autowired(required = false)
	public void setConfigurers(List<WebMvcConfigurer> configurers) {
		if (!CollectionUtils.isEmpty(configurers)) {
			this.configurers.addWebMvcConfigurers(configurers);
		}
	}
	

所以,我们继承了WebMvcConfigurationSupport,而后使用@EnableWebMvc会覆盖掉原来的配置。

2、WebMvcConfigurer

WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的 xml 配置文件形式进行针对框架个性化定制。可以配置多个
WebMvcConfigurer的主要方法有:

  • configurePathMatch:配置路由请求规则
  • configureContentNegotiation:内容协商配置
  • configureAsyncSupport configureDefaultServletHandling:默认静态资源处理器
  • addFormatters:注册自定义转化器
  • addInterceptors:拦截器配置
  • addResourceHandlers:资源处理
  • addCorsMappings:CORS配置
  • addViewControllers:视图跳转控制器
  • configureViewResolvers:配置视图解析
  • addArgumentResolvers:添加自定义方法参数处理器
  • addReturnValueHandlers:添加自定义返回结果处理器
  • configureMessageConverters:配置消息转换器。重载会覆盖默认注册的HttpMessageConverter
  • extendMessageConverters:配置消息转换器。仅添加一个自定义的HttpMessageConverter.
  • configureHandlerExceptionResolvers:配置异常转换器
  • extendHandlerExceptionResolvers:添加异常转化器 getValidator
  • getMessageCodesResolver
addInterceptors:拦截器

addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
addPathPatterns:用于设置拦截器的过滤路径规则;addPathPatterns("/**")对所有请求都拦截
excludePathPatterns:用于设置不需要拦截的过滤规则
拦截器主要用途:进行用户登录状态的拦截,日志的拦截等。

@Override
public void addInterceptors(InterceptorRegistry registry) {
    super.addInterceptors(registry);
    registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/login","/js/**","/css/**","/images/**");
}

@EnableWebMvc使用方式

配合上MVC 配置类 再加上@EnableWebMvc 就是相当于 屏蔽掉 @EnableAutoConfiguration中的设置

  • 使用@EnableWebMvc注解 等于 扩展了WebMvcConfigurationSupport,但是没有重写任何方法

  • 使用“extends WebMvcConfigurationSupport”方式(需要添加@EnableWebMvc),会屏蔽掉springBoot的@EnableAutoConfiguration中的设置

  • 使用“implement WebMvcConfigurer”可以配置自定义的配置,同时也使用了@EnableAutoConfiguration中的设置

  • 使用“implement WebMvcConfigurer + @EnableWebMvc”,会屏蔽掉springBoot的@EnableAutoConfiguration中的设置

@EnableAutoConfiguratio 是什么?

这里的“@EnableAutoConfiguration中的设置”是指,读取 application.properties 或 application.yml 文件中的配置。
所以:
如果需要使用springBoot的@EnableAutoConfiguration中的设置,那么就只需要“implement WebMvcConfigurer”即可。
如果,需要自己扩展同时不使用@EnableAutoConfiguration中的设置,可以选择另外的方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值