• 使用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
  • 相关阅读:
    Revit中顶对齐风管的绘制及“管线自动对齐”功能
    FFmpeg入门详解之31:Ubuntu编译FFmpeg
    如何在Centos8中添加附加的IP
    JavaScript——WebAPI
    高压功率放大器在超声悬浮中的应用研究
    node对接微信支付,微信返回失败
    Aop踩坑!记一次模板类调用注入属性为空的问题
    vue.js毕业设计,基于vue.js前后端分离订座预约小程序系统 开题报告
    Windows原理深入学习系列-访问控制列表
    基于51单片机的温度控制恒温箱设计
  • 原文地址:https://blog.csdn.net/weixin_43811057/article/details/132872283