listener、filter、servlet、interceptor在springboot中执行顺序

1.前提

需要在springboot中提前分别注册listenerfilterservletinterceptor,代码可参考以下文章:


2.测试

本地访问http://localhost:8080/my,查看日志信息,如下:

2020-11-18 21:07:09.523  INFO 6536 --- [ost-startStop-1] com.xzl.spire.filter.MyFilter            : MyFilter...init
2020-11-18 21:07:09.777  INFO 6536 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-11-18 21:07:10.065  INFO 6536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@168660: startup date [Wed Nov 18 21:07:06 CST 2020]; root of context hierarchy
2020-11-18 21:07:10.150  INFO 6536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/book],methods=[GET]}" onto public com.xzl.spire.model.Book com.xzl.spire.controller.BookController.book()
2020-11-18 21:07:10.152  INFO 6536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/delete/{id}],methods=[POST]}" onto public java.lang.String com.xzl.spire.controller.BookController.deleteBook(java.lang.Long)
2020-11-18 21:07:10.154  INFO 6536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/getUserById],methods=[GET]}" onto public java.lang.String com.xzl.spire.controller.UserController.getUserById(java.lang.Integer)
2020-11-18 21:07:10.154  INFO 6536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/deleteUserById],methods=[GET]}" onto public void com.xzl.spire.controller.UserController.deleteUserById(java.lang.Integer)
2020-11-18 21:07:10.156  INFO 6536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto 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-11-18 21:07:10.156  INFO 6536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-11-18 21:07:10.219  INFO 6536 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-11-18 21:07:10.219  INFO 6536 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-11-18 21:07:10.474  INFO 6536 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-11-18 21:07:10.515  INFO 6536 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-18 21:07:10.520  INFO 6536 --- [           main] com.xzl.spire.SpireApplication           : Started SpireApplication in 4.597 seconds (JVM running for 8.211)
2020-11-18 21:09:18.635  INFO 6536 --- [nio-8080-exec-1] com.xzl.spire.listener.MyListener        : MyListener...requestInitialized
2020-11-18 21:09:18.650  INFO 6536 --- [nio-8080-exec-1] com.xzl.spire.filter.MyFilter            : MyFilter...doFilter
2020-11-18 21:09:18.651  INFO 6536 --- [nio-8080-exec-1] com.xzl.spire.servlet.MyServlet          : doGet...
2020-11-18 21:09:18.662  INFO 6536 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2020-11-18 21:09:18.663  INFO 6536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2020-11-18 21:09:18.687  INFO 6536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 24 ms
2020-11-18 21:09:18.695  INFO 6536 --- [nio-8080-exec-1] com.xzl.spire.interceptor.MyInterceptor  : MyInterceptor>>>preHandle
2020-11-18 21:09:18.731  INFO 6536 --- [nio-8080-exec-1] com.xzl.spire.interceptor.MyInterceptor  : MyInterceptor>>>postHandle
2020-11-18 21:09:18.790  INFO 6536 --- [nio-8080-exec-1] com.xzl.spire.interceptor.MyInterceptor  : MyInterceptor>>>afterCompletion
2020-11-18 21:09:18.815  INFO 6536 --- [nio-8080-exec-1] com.xzl.spire.listener.MyListener        : MyListener...requestDestroyed

再down掉项目后,新产生的日志信息如下:

2020-11-18 21:31:20.558  INFO 6536 --- [      Thread-18] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@168660: startup date [Wed Nov 18 21:07:06 CST 2020]; root of context hierarchy
2020-11-18 21:31:20 JRebel: Reconfiguring bean 'myWebMvcConfig' [com.xzl.spire.config.MyWebMvcConfig$$EnhancerBySpringCGLIB$$5db458ff]
2020-11-18 21:31:20.889  INFO 6536 --- [      Thread-18] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2020-11-18 21:31:21.039  INFO 6536 --- [ost-startStop-2] com.xzl.spire.filter.MyFilter            : MyFilter...destroy

3.结论

执行顺序如下:
1.项目 run时,filter开始init
2.浏览器发送get请求,listener开始init
3.filter执行doFilter方法
4.servlet执行doGet方法
5.interceptor开始依次执行preHandlepostHandleafterCompletion方法
6.listener执行requestDestroyed方法,get请求结束
7.项目down掉,filter执行destroy方法

推荐阅读更多精彩内容