圖解原始碼 | SpringBoot中拓展SpringMVC原理
@Configuration public class FeichaoMvcConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new HandlerInterceptor() { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("公眾號:肥朝(id:feichao_java),preHandle"); return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("公眾號:肥朝(id:feichao_java),postHandle"); } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("公眾號:肥朝(id:feichao_java),afterCompletion"); } }); } }
圖解原始碼
整個過程肥朝小聲逼逼幾句:
1.根據 spring.factories
上的自動配置,載入 WebMvcAutoConfiguration
,根據序號流程我們知道,最後會把所有的 WebMvcConfigurer
,都載入進名為 delegates
的陣列中。我們自定義的 FeichaoMvcConfig
就是 WebMvcConfigurer
的子類。
2. WebMvcConfigurationSupport
是 DelegatingWebMvcConfiguration
的父類。從序號流程我們知道,此時就是把第一步的 delegates
陣列,遍歷( WebMvcConfigurer
)呼叫 addInterceptor
。addInterceptor這個是SpringMvc的api,從單詞名字,我們也很容易知道其意圖,這就是所謂的,好的程式碼會說話。
3.幻想無數,不如動次真格!自己對著上面的圖除錯一次,箭頭和序號都給你標記好了,如果再有人再怎麼看原始碼,請把這個 圖解原始碼系列
甩給他。
寫在最後
111111111111111111111111111111111111111 1 1 1111111111111111 1 1 1111111111111111111