@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");
}
});
}
}
整個過程肥朝小聲逼逼幾句:java
1.根據
spring.factories
上的自動配置,加載WebMvcAutoConfiguration
,根據序號流程咱們知道,最後會把全部的WebMvcConfigurer
,都加載進名爲delegates
的數組中。咱們自定義的FeichaoMvcConfig
就是WebMvcConfigurer
的子類。spring2.
WebMvcConfigurationSupport
是DelegatingWebMvcConfiguration
的父類。從序號流程咱們知道,此時就是把第一步的delegates
數組,遍歷(WebMvcConfigurer
)調用addInterceptor
。addInterceptor這個是SpringMvc的api,從單詞名字,咱們也很容易知道其意圖,這就是所謂的,好的代碼會說話。api3.幻想無數,不如動次真格!本身對着上面的圖調試一次,箭頭和序號都給你標記好了,若是再有人再怎麼看源碼,請把這個
圖解源碼系列
甩給他。數組
111111111111111111111111111111111111111111111111111111111111111111111111111111ide