文章目录
-
-
- 一、 问题分析
-
-
二、 解决 WebMvcConfigurationSupport 静态资源失效问题的办法
一、 问题分析
从上图可以看出,WebMvcConfigurationAdapter 在spring boot 2.0被废弃了。
当选择继承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 {
public static final String DEFAULT_PREFIX = “”;
public static final String DEFAULT_SUFFIX = “”;
private static final String[] SERVLET_LOCATIONS = { “/” };
@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = “spring.mvc.hiddenmethod.filter”, name = “enabled”, matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
从源码看出容器中有@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)这个注解。
当容器中没有这个组件的时候,这个自动配置类才生效
因此当选择继承 WebMvcConfigurationSupport 以后还需要 重写它里面的方法。
然而问题又出来了,这样的继承会导致静态资源失效。
如何解决这个问题?
/**
-
An implementation of {@link WebMvcConfigurer} with empty methods allowing
-
subclasses to override only the methods they’re interested in.
-
@author Rossen Stoyanchev
-
@since 3.1
-
@deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made
-
possible by a Java 8 baseline) and can be implemented directly without the
-
need for this adapter
*/
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
最后
在面试前我整理归纳了一些面试学习资料,文中结合我的朋友同学面试美团滴滴这类大厂的资料及案例
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
大家看完有什么不懂的可以在下方留言讨论也可以关注。
觉得文章对你有帮助的话记得关注我点个赞支持一下!
Ct994Orb-1726574888255)]
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
大家看完有什么不懂的可以在下方留言讨论也可以关注。
觉得文章对你有帮助的话记得关注我点个赞支持一下!