Spring MVC源码分析——初始化过程

1.      概述

对于Web开发者,MVC模型是大家再熟悉不过的了,SpringMVC中,满足条件的请求进入到负责请求分发的DispatcherServlet,DispatcherServlet根据请求url到控制器的映射(HandlerMapping中保存),HandlerMapping最终返回HandlerExecutionChain,其中包含了具体的处理对象handler(也即我们编程时写的controller)以及一系列的拦截器interceptors,此时DispatcherServlet会根据返回的HandlerExecutionChain中的handler找到支持这一处理器类型的适配器(handlerAdapter),在处理器适配器中最终会去调用控制器的请求响应方法并返回结果视图(ModelAndView),得到结果视图后,通过render方法完成结果的显示。

HanderMapping的继承体系:

1

 

HandlerAdapter的继承体系:

2

 

同样的视图解析器ViewResolver针对不同的输出格式也有一系列的实现类,具体可自己看。

 

2.    实现分析

以我自己的一个web项目中spring mvc的配置为例:

 
 
  1. <context:component-scan base-package="cn.ds.log" />
  2.  
  3. <bean id="defaultHandlerMapping"
  4. class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
  5.  
  6. <bean
  7. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  8. </bean>
  9.  
  10. <bean
  11. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  12. </bean>
  13.  
  14. <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter">
  15. </bean>
  16.  
  17. <bean id="viewResolver"
  18. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  19. <property name="prefix">
  20. <value>/WEB-INF/jsp/</value>
  21. </property>
  22. <property name="suffix">
  23. <value>.jsp</value>
  24. </property>
  25. </bean>

这里因为是采用全注解的方式,所以先通过context:component-scan配置让spring自定扫描的包路径,接着配置handlerMapping、handlerAdapter及ViewResolver,几乎包含了SpringMVC的配置中需要涉及的所有元素。后面需要涉及具体的HanderMapping等的实现时,将以这里配置中的实现为例进行分析,其它的大家“同理可解”。⊙﹏⊙b汗

 

2.1  Spring MVC初始化流程

DispatcherServlet的继承体系如:

3

看到它们继承自HttpServlet,你就知道初始化过程应该是从init方法开始了,整个初始化的流程为:

4

 

很简单是么?我也觉得是,至少从上面的时序图看来是这样,不过前提是你已经很了解Spring IOC的实现原理了。上面的时序图中,在5的initStragegies()中除了调用6,7的函数外,还有几个类似的初始化函数,因为这里主要是为了理解整个的流程,所以我都省略了。上面流程可能需要分析的地方就在于3,4步,我们看看initWebApplicationContext函数的实现:

 
 
  1. protected WebApplicationContext initWebApplicationContext() {
  2. WebApplicationContext rootContext =
  3. WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  4. WebApplicationContext wac = null;
  5.  
  6. if (this.webApplicationContext != null) {
  7. // A context instance was injected at construction time -&gt; use it
  8. wac = this.webApplicationContext;
  9. if (wac instanceof ConfigurableWebApplicationContext) {
  10. ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
  11. if (!cwac.isActive()) {
  12. ……
  13. configureAndRefreshWebApplicationContext(cwac);
  14. }
  15. }
  16. }
  17. if (wac == null) {
  18. wac = findWebApplicationContext();
  19. }
  20. if (wac == null) {
  21. // No context instance is defined for this servlet -&gt; create a local one
  22. wac = createWebApplicationContext(rootContext);
  23. }
  24.  
  25. if (!this.refreshEventReceived) {
  26. // Either the context is not a ConfigurableApplicationContext with refresh
  27. // support or the context injected at construction time had already been
  28. // refreshed -&gt; trigger initial onRefresh manually here.
  29. onRefresh(wac);
  30. }
  31.  
  32. if (this.publishContext) {
  33. ……
  34. }
  35.  
  36. return wac;
  37. }

 

看起来貌似有点复杂,其实理解了IOC容器的实现原理(可以看下“spring ioc源码分析”一文,⊙﹏⊙多年前弄的,这次暑假实习时又以读书报告的形式写了,感觉当年肿么可以写得这么乱……也是一种成长,不打算修改)就很简单,函数一开始会去获取WebApplicationContext对象,这个对象在ContextLoaderListener初始化IOC容器时就已经把它set到ServletContext的属性中,而且它也正是ConfigurableWebApplicationContext的实例,第一个if语句其实就是如果此时SpringIOC容器没有初始化的话就在这里启动IOC容器的初始化过程,因为看“省略(1)”中的代码你就知道,它会在这里调用refresh函数,“世人”都知道这就是IOC容器启动的入口,这里会解析配置文件springmvc-servlet.xml。

这里最终要执行onRefresh(),而这个就是SpringMVC初始化的入口。

(注:其实这里也可以配置log4j,通过其打印的info信息来看IOC与MVC的初始化顺序)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值