微信公衆號:I am CR7
若有問題或建議,請在下方留言;
最近更新:2018-12-28java
有些日子沒寫文章了,各位,很久不見。前些天忙着給公司業務部門作了一套spring cloud netflix zuul集成spring security oauth2.0的定製版方案,連續加了N天班,實在是沒時間繼續以前Eureka源碼分析,暫時先擱置下來吧。爲了更好的理解zuul,今天趁着有時間,對它的源碼作一次詳細分析。今天發佈的這一篇文章,做爲預熱篇,僅僅是滿漢全席前的"開胃小菜",您看着品品,哈哈。web
DispatcherServlet做爲SpringMVC最核心的類,它的源碼咱們有必要仔細閱讀。作過WEB開發的同窗,或多或少都知道一些Servlet相關的知識。對於它的初始化過程,你知道多少呢?
請看:spring
簡單看下初始化代碼:微信
1@Override
2protected void onRefresh(ApplicationContext context) {
3 initStrategies(context);
4}
5
6/**
7 * Initialize the strategy objects that this servlet uses.
8 * <p>May be overridden in subclasses in order to initialize further strategy objects.
9 */
10protected void initStrategies(ApplicationContext context) {
11 initMultipartResolver(context);
12 initLocaleResolver(context);
13 initThemeResolver(context);
14 initHandlerMappings(context);
15 initHandlerAdapters(context);
16 initHandlerExceptionResolvers(context);
17 initRequestToViewNameTranslator(context);
18 initViewResolvers(context);
19 initFlashMapManager(context);
20}
複製代碼
這裏與zuul請求處理相關的關鍵方法是initHandlerMappings()和initHandlerAdapters()方法。mvc
關於DispatcherServlet初始化過程,我畫了一個時序圖,特意畫出了重要的兩個方法。
app
日誌做爲咱們閱讀源碼的利器之一,萬萬不可忽視,這裏是初始化過程打印的debug日誌,參照上面的時序圖,能夠加深理解。ide
12018-12-27 15:41:25.752 [http-nio-8558-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Initializing servlet 'dispatcherServlet'
2
32018-12-27 15:41:29.777 [http-nio-8558-exec-1] INFO o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'
42018-12-27 15:41:30.956 [SimpleHostRoutingFilter.connectionManagerTimer] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Closing expired connections
52018-12-27 15:41:30.959 [http-nio-8558-exec-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started
62018-12-27 15:41:35.246 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'multipartResolver'
72018-12-27 15:41:35.246 [http-nio-8558-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Using MultipartResolver [org.springframework.web.multipart.support.StandardServletMultipartResolver@4d406619]
82018-12-27 15:41:35.253 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver'
92018-12-27 15:41:35.254 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
102018-12-27 15:41:35.297 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
112018-12-27 15:41:35.298 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
122018-12-27 15:41:35.307 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver'
132018-12-27 15:41:35.308 [http-nio-8558-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@49f4819a]
142018-12-27 15:41:35.318 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver'
152018-12-27 15:41:35.319 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
162018-12-27 15:41:35.351 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
172018-12-27 15:41:35.352 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
182018-12-27 15:41:35.357 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver'
192018-12-27 15:41:35.357 [http-nio-8558-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver@68903980]
202018-12-27 15:41:46.543 [SimpleHostRoutingFilter.connectionManagerTimer] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Closing expired connections
212018-12-27 15:41:47.584 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'faviconHandlerMapping'
222018-12-27 15:41:47.585 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'requestMappingHandlerMapping'
232018-12-27 15:41:47.585 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'viewControllerHandlerMapping'
242018-12-27 15:41:47.585 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'beanNameHandlerMapping'
252018-12-27 15:41:47.586 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'resourceHandlerMapping'
262018-12-27 15:41:47.586 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'defaultServletHandlerMapping'
272018-12-27 15:41:47.586 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'welcomePageHandlerMapping'
282018-12-27 15:41:47.587 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'zuulHandlerMapping'
292018-12-27 15:41:47.588 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'webEndpointServletHandlerMapping'
302018-12-27 15:41:47.588 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'controllerEndpointHandlerMapping'
312018-12-27 15:41:50.696 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'requestMappingHandlerAdapter'
322018-12-27 15:41:50.697 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'httpRequestHandlerAdapter'
332018-12-27 15:41:50.697 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'simpleControllerHandlerAdapter'
342018-12-27 15:41:52.972 [SimpleHostRoutingFilter.connectionManagerTimer] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Closing expired connections
352018-12-27 15:41:52.974 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'errorAttributes'
362018-12-27 15:41:52.974 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'handlerExceptionResolver'
372018-12-27 15:41:52.974 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
382018-12-27 15:41:52.974 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
392018-12-27 15:41:52.980 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
402018-12-27 15:41:52.980 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
412018-12-27 15:41:52.981 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
422018-12-27 15:41:52.981 [http-nio-8558-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@1cfbb4c8]
432018-12-27 15:41:52.982 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'beanNameViewResolver'
442018-12-27 15:41:52.982 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'mvcViewResolver'
452018-12-27 15:41:52.982 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'defaultViewResolver'
462018-12-27 15:41:52.982 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'viewResolver'
472018-12-27 15:41:52.983 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
482018-12-27 15:41:52.983 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
492018-12-27 15:41:52.990 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
502018-12-27 15:41:52.990 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
512018-12-27 15:41:52.991 [http-nio-8558-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
522018-12-27 15:41:52.991 [http-nio-8558-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@6d57a78b]
532018-12-27 15:41:52.991 [http-nio-8558-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet]
54
55
562018-12-27 15:41:52.991 [http-nio-8558-exec-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 21059 ms
572018-12-27 15:41:52.991 [http-nio-8558-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Servlet 'dispatcherServlet' configured successfully
複製代碼
本文內容很少,幾個字總結,那就是一個圖兩個方法。也許有人會問,這麼點東西不必單獨發一篇文章吧?其實否則,理解了這一部分,對於後面看DispatcherServlet處理請求時如何進入ZuulController,走到ZuulServlet,是很是有幫助的。不信,下一篇文章發出來的時候,您能夠體會體會。
很久不寫,確實有些生疏了,好習慣仍是得堅持。筆者不才,有任何意見或者建議,但願你們留言一塊兒討論,十分感謝!!!
最後,借吳軍老師谷歌方法論經常使用的結束方式:祝順利,2018年12月28號,祁琛。源碼分析