springboot拦截器注入bean失败实例

  之前使用拦截器做了一些处理,详见springboot拦截器配置、消息头校验、重复请求过滤

  如今想加入一些处理,需要在spring拦截器中注入了一个service,结果运行时报了空指针:

2020-01-03 09:26:33.646 |-DEBUG [http-nio-9988-exec-3] org.springframework.web.servlet.DispatcherServlet [1100] -| Failed to complete request: java.lang.NullPointerException
2020-01-03 09:26:33.648 |-ERROR [http-nio-9988-exec-3] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] [175] -| Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
    at com.wlf.order.prize.aop.RequestInterceptor.postHandle(RequestInterceptor.java:118)
    at org.springframework.web.servlet.HandlerExecutionChain.applyPostHandle(HandlerExecutionChain.java:156)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1046)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
2020-01-03 09:26:33.657 |-DEBUG [http-nio-9988-exec-3] org.springframework.web.servlet.DispatcherServlet [91] -| "INCLUDE" dispatch for POST "/error", parameters={}
2020-01-03 09:26:33.661 |-DEBUG [http-nio-9988-exec-3] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping [420] -| Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-01-03 09:26:33.662 |-DEBUG [http-nio-9988-exec-3] org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor [86] -| Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2020-01-03 09:26:33.664 |-DEBUG [http-nio-9988-exec-3] org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor [223] -| Found 'Content-Type:application/json;charset=UTF-8' in response
2020-01-03 09:26:33.669 |-DEBUG [http-nio-9988-exec-3] org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor [91] -| Writing [{timestamp=Fri Jan 03 09:26:33 CST 2020, status=200, error=OK, message=No message available, path=/j (truncated)...]
2020-01-03 09:26:33.685 |-DEBUG [http-nio-9988-exec-3] org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor [111] -| Closing JPA EntityManager in OpenEntityManagerInViewInterceptor

 

  按日志提示,到RequestInterceptor.java的118行看一下:

 

 

  发现我们通过@Autowired注入进来的mailService并未实例化:

 

 

  而mailService本身是被spring容器管理的

 

 

  看起来都在spring容器的管控范围内,为啥没有实例化呢?还得看看拦截器是怎么实例化的

 

 

  很明显,这里对拦截器的实例化并不在spring容器的管控之内,它是new出来的。由此可以推断,我们只需要把拦截器的实例化让spring容器来做即可,也就是放开上面注掉的代码

 

 

  再次调用接口,拦截器的实例化交给了spring容器,容器自会管理起mailService的注入,再无空指针异常。

posted on 2020-01-03 10:05  不想下火车的人  阅读(2779)  评论(0编辑  收藏  举报

导航