• 使用HandlerInterceptor 中注入其他service时为null分析及解决


    一情况分析

    1.1 拦截器代码

    public class ServerInterceptor implements HandlerInterceptor {
        private static final Logger _logger = LoggerFactory.getLogger(ServerInterceptor.class);
        PrintWriter out;
        JSONObject res = new JSONObject();
        
        @Autowired
        UserInfoLoginService userInfoLoginService;
        @Autowired
        SysUserService sysUserService;
        
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            _logger.info("ServerInterceptor逻辑处理");
            String header = request.getHeader("Server");
            if (!"Server".equals(header)) {
                _logger.info("ServerInterceptor无权限");
                res.put("code", "Server");
                res.put("msg", "ServerInterceptor无权限");
                res.put("data", null);
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            return true;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    1.2 拦截器配置

    在这里插入图片描述

    1.3 原因

    造成 UserInfoLoginService,SysUserService 为null的原因:
    拦截器加载是在springcontext创建之前完成的,在拦截器配置进行加载,进行new ServerInterceptor()时,这里只是创建的ServerInterceptor空对象,并没有将内部属性进行注入;容器启动结束在使用拦截器时注入实体自然就为null。

    二解决方案

    在拦截器配置中将拦截器提前暴露,交给spring容器管理

    @Configuration
    public class InterceptorConfig implements WebMvcConfigurer {
        @Resource
        InterceptorProperties interceptorProperties;
    
        @Resource
        ServerInterceptor serverInterceptor;
    
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
    
    
            //注册ServerInterceptor
            InterceptorRegistration serverRegistration = registry.addInterceptor(serverInterceptor);
    
            if (!CollectionUtils.isEmpty(interceptorProperties.getIncludeFilterServerPaths())) {
                // Server拦截路径
               serverRegistration.addPathPatterns(interceptorProperties.getIncludeFilterServerPaths());
            }
           
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2、拦截器添加@Component,交给容器管理

    @Component
    public class ServerInterceptor implements HandlerInterceptor {
        private static final Logger _logger = LoggerFactory.getLogger(ServerInterceptor.class);
        PrintWriter out;
        JSONObject res = new JSONObject();
    
        @Autowired
        UserInfoLoginService userInfoLoginService;
    
        @Autowired
        SysUserService sysUserService;
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            _logger.info("ServerInterceptor逻辑处理");
            String header = request.getHeader("Server");
            if (!"Server".equals(header)) {
                _logger.info("ServerInterceptor无权限");
                res.put("code", "Server");
                res.put("msg", "ServerInterceptor无权限");
                res.put("data", null);
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            return true;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    【车间调度】基于GA/PSO/SA/ACO/TS优化算法的车间调度比较(Matlab代码实现)
    SANSAN每周新鲜事|OPC UA 数据采集,你真的了解吗?
    python基础(5):深入理解 python 中的赋值、引用、拷贝、作用域
    ESP8266-Arduino编程实例-OLED显示QR码(二维码)
    echarts图表中显示图例lengend
    网络安全笔记-加解密算法
    anaconda虚拟环境常用指令记录
    Node.js 模块系统
    最高效面试八股文,平躺80%的企业
    机器学习中的 朴素贝叶斯算法及其优缺点
  • 原文地址:https://blog.csdn.net/weixin_43811057/article/details/132872283