使用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 拦截器配置

使用HandlerInterceptor 中注入其他service时为null分析及解决_第1张图片

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());
        }
       


    }
}

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;
    }

}

你可能感兴趣的:(#,SpringBoot,#,异常,开发语言,java)