WebMvcConfigurationSupport是Spring Boot2.0以后用来替代WebMvcConfigurerAdapter,但是如果直接使用WebMvcConfigurationSupport替换掉WebMvcConfigurerAdapter会出现各种问题。

原因就是使用WebMvcConfigurationSupport时只是使用了SpringMVC的基本配置,webmvc自动化配置就会失效,导致静态资源使用不了。需要在继承类中处理。

如:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
         //registry.addResourceHandler("/static/*/**").addResourceLocations("classpath:/static/");
        //重写这个方法,映射静态资源文件
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/")
                ;
        super.addResourceHandlers(registry);
 
    }

最好的方法是:使用implements WebMvcConfigure替换extends WebMvcConfigurationSupport,这样就就可以在原来的自动化配置上扩展自己的功能。

 

 posted on 2020-11-28 16:49  会飞的金鱼  阅读(1127)  评论(0)    收藏  举报