图解源码 | 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.幻想无数,不如动次真格!自己对着上面的图调试一次,箭头和序号都给你标记好了,如果再有人再怎么看源码,请把这个图解源码系列甩给他。
(想自学习编程的小伙伴请搜索圈T社区,更多行业相关资讯更有行业相关免费****等待你来学习。完全免费哦! )