WebMvcConfigurerAdapter与WebMvcConfigurer与WebMvcConfigurationSupport与WebMvcAutoConfiguration添加拦截器

首先以上4个类都是springboot中mvc的支持类

  1. WebMvcConfigurer 为接口
  2. WebMvcConfigurerAdapter 是 WebMvcConfigurer 的实现类大部分为空方法 , 已经废除
  3. WebMvcConfigurationSupport 是mvc的基本实现并包含了WebMvcConfigurer接口中的方法
  4. WebMvcAutoConfiguration 是mvc的自动装在类并部分包含了WebMvcConfigurer接口中的方法

如果在springboot项目中没有使用到以上类,那么会自动启用WebMvcAutoConfiguration类做自动加载
项目中的配置都是默认的,比如静态资源文件的访问。

如果项目中要添加自己的拦截器addInterceptors

  • 方法1继承WebMvcConfigurationSupport
    会导致WebMvcAutoConfiguration不加载,所以默认配置丢失导致静态资源等无法访问
@Configuration
public class WebMvc extends WebMvcConfigurationSupport {
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		UserInterceptor userInterceptor = new UserInterceptor();
		registry.addInterceptor(userInterceptor)
				.addPathPatterns("/user/**");
	}
   //添加此方法恢复静态资源访问
   @Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/resources/")
				.addResourceLocations("classpath:/static/")
				.addResourceLocations("classpath:/public/");
	}
}
  • 方法2 实现继承WebMvcConfigurer
    因为是接口实现所以WebMvcAutoConfiguration还会默认配置,所以静态资源是可以访问的
@Configuration
public class WebMvc implements WebMvcConfigurer {
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		UserInterceptor userInterceptor = new UserInterceptor();
		registry.addInterceptor(userInterceptor)
				.addPathPatterns("/user/**");
	}
}

所以现实不现实静态资源关键是在WebMvcAutoConfiguration,如果没有加载就需要自己添加方法,看一下WebMvcAutoConfiguration静态资源加载的方法实现

@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 {
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (!this.resourceProperties.isAddMappings()) {
			logger.debug("Default resource handling disabled");
			return;
		}
		Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
		CacheControl cacheControl = this.resourceProperties.getCache()
				.getCachecontrol().toHttpCacheControl();
		if (!registry.hasMappingForPattern("/webjars/**")) {
			customizeResourceHandlerRegistration(registry
					.addResourceHandler("/webjars/**")
					.addResourceLocations("classpath:/META-INF/resources/webjars/")
					.setCachePeriod(getSeconds(cachePeriod))
					.setCacheControl(cacheControl));
		}
		//静态资源  staticPathPattern 值为 /**
		String staticPathPattern = this.mvcProperties.getStaticPathPattern();
		if (!registry.hasMappingForPattern(staticPathPattern)) {
			customizeResourceHandlerRegistration(
					registry.addResourceHandler(staticPathPattern)
							.addResourceLocations(getResourceLocations(
									this.resourceProperties.getStaticLocations()))
							.setCachePeriod(getSeconds(cachePeriod))
							.setCacheControl(cacheControl));
		}
	}
}

// getStaticLocations() 值为
//“classpath:/META-INF/resources/”, “classpath:/resources/”,
//“classpath:/static/”, “classpath:/public/” 跟我们手动配置的一样

soso 希望能帮到你

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值