Analysis and resolution of null when injecting other services using HandlerInterceptor

A situation analysis

1.1 Interceptor code

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 Interceptor configuration

Insert image description here

1.3 Reasons

Reasons why UserInfoLoginService and SysUserService are null:
The interceptor loading is completed before the springcontext is created. When the interceptor configuration is loaded and new ServerInterceptor() is performed, there is only an empty ServerInterceptor object created, and no internal attributes are injected; the container is started and injected when the interceptor is used. The entity is naturally null.

Two solutions

Expose the interceptor in advance in the interceptor configuration and leave it to the spring container management

@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. Add @Component to the interceptor and hand it over to the container for management.

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

}

Guess you like

Origin blog.csdn.net/weixin_43811057/article/details/132872283