zoukankan      html  css  js  c++  java
  • 【MARK】拦截器中自动注入失败问题

    我在拦截器中想自动注入一个对象的时候发现无法注入,获取到的一直是null

    public class RestInterceptor implements HandlerInterceptor {
        
        @Autowired
        private EscUserMapper escUserMapper;
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws BaseAppException {
            return true;
        }
        
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws BaseAppException {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws BaseAppException {
            // TODO Auto-generated method stub
            
        }
    }
    

      

    解决办法:
    在Spring添加拦截器之前先自己创建一下这个Spring Bean,这样就能在Spring映射这个拦截器前自动注入对象了。

    @Configuration
    public class RestWebAppConfigurer extends WebMvcConfigurerAdapter {
        
        @Bean
        public RestInterceptor restInterceptor() {
            return new RestInterceptor();
        }
    
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(restInterceptor()).addPathPatterns("/iot/**");
            super.addInterceptors(registry);
        }
    }
    

      

    参考文档:http://www.cnblogs.com/niceboat/p/6958895.html

  • 相关阅读:
    Linux 磁盘与文件系统管理
    mysql join
    iostat
    解决TIME_WAIT过多问题
    mysql 数据库性能追踪与分析
    CHECKPOINT
    DTRACE -MYSQL
    ORCLE INNODB 博客与 innodb_lru_scan_depth
    innodb 变量
    mysql博客
  • 原文地址:https://www.cnblogs.com/luoluocaihong/p/7347653.html
Copyright © 2011-2022 走看看