zoukankan      html  css  js  c++  java
  • HandlerInterceptor注入bean报错

    过滤器注入Bean

    /**
     * 登录拦截器
     * @author swt
     */
    @Component
    public class AccessTokenInterceptor implements HandlerInterceptor {
    
        @Autowired
        private LoginInfoService loginInfoService;
    
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        // loginInfoService = null ???
            
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
            // 请求处理之后进行调用,但是在视图被渲染之前
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
            // 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
        }
    
    }
    

      

    错误的写法

    /**
     * 过滤器
     * @author swt
     */
    @Configuration
    public class MvcConfiguration extends WebMvcConfigurerAdapter {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new AccessTokenInterceptor ()).addPathPatterns("/**");
        }
    
    }
    

      

    正确的写法

    /**
     * 过滤器
     * @author swt
     */
    @Configuration
    public class MvcConfiguration extends WebMvcConfigurerAdapter {
    
        @Autowired
        private AccessTokenInterceptor accessTokenInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(accessTokenInterceptor).addPathPatterns("/**");
        }
    
    }
    

      

  • 相关阅读:
    Spark在MaxCompute的运行方式
    新功能初探 | MySQL 8.0 Multi-Valued Indexes功能简述
    吐血整理 | 1000行MySQL学习笔记,不怕你不会,就怕你不学!
    阿里巴巴架构师:十问业务中台和我的答案
    C# int?
    页面后退清空缓存
    oracle 中 创建序列sequence
    sql 与 oracle 几个简单语法差别
    oracle 中用法dual
    将DataTable进行分页并生成新的DataTable
  • 原文地址:https://www.cnblogs.com/song-wentao/p/8318189.html
Copyright © 2011-2022 走看看