SpringBoot学习之路---踩的关于WebMvcConfigurationSupport的小坑

最近在敲一个SpringBoot的练手项目,想自定义配置一个拦截器的,用来判断用户有没有登录,于是打算继承WebMvcConfigurerAdapter,然后来复写一些方法,结果SpringBoot5直接说已经过时了,推荐用WebMvcConfigurationSupport,于是一整天的找坑之路…


public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (request.getSession().getAttribute("user") == null) {
            request.setAttribute("msg","您还未登录");
            request.getRequestDispatcher("/").forward(request,response);
            return false;
        }
        else
        return true;
    }

自定义了一个拦截器,编写好了具体方法,最后加入到配置类中了,结果怎么都扫描不到

配置类相关代码:

@Bean
    public WebMvcConfigurationSupport webMvcConfigurationSupport(){
        return new CurdWebMvcConfigurationSupport();
    }


最后上网找了方法,自己去查看了WebMvcAutoConfiguration的源码。

好家伙!!!

@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})

发现这个注解,这个注解意思是,如果你的容器内有了WebMvcConfigurationSupport这个类的话,那么这个自动配置类就不会生效了,别的组件你可能需要自己配,其中还包括静态资源的映射。


解决方法

冤有头债有主,我还是换回了WebMvcConfigrerAdapter,如果我们需要在SpringBoot为我们自动配置好相关组件的基础上,再额外添加一些组件,还是推荐使用WebMvcConfigrerAdapter

那么它过时的意义在哪里勒,为什么官方推荐WebMvcConfigrationSupport呢?抱歉,我也没搞懂,如果大家有知道原因,希望大佬们指点一下,感激不尽!

发布了38 篇原创文章 · 获赞 46 · 访问量 7542

猜你喜欢

转载自blog.csdn.net/Jokeronee/article/details/105150316