Spring Boot 实战(5)解决 WebMvcConfigurationSupport 静态资源失效问题

文章目录

      • 一、 问题分析
  • 二、 解决 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 {

最后

在面试前我整理归纳了一些面试学习资料,文中结合我的朋友同学面试美团滴滴这类大厂的资料及案例

MyBatis答案解析
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

大家看完有什么不懂的可以在下方留言讨论也可以关注。

觉得文章对你有帮助的话记得关注我点个赞支持一下!
Ct994Orb-1726574888255)]
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

大家看完有什么不懂的可以在下方留言讨论也可以关注。

觉得文章对你有帮助的话记得关注我点个赞支持一下!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值