首先以上4个类都是springboot中mvc的支持类
WebMvcConfigurer 为接口
WebMvcConfigurerAdapter 是 WebMvcConfigurer 的实现类大部分为空方法 , 已经废除
WebMvcConfigurationSupport 是mvc的基本实现并包含了WebMvcConfigurer接口中的方法
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/” 跟我们手动配置的一样
————————————————
版权声明:本文为CSDN博主「A 糖醋排骨」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43153309/article/details/88639130