spring框架提供了构建web应用程序的全功能mvc模块,叫spring mvc,通过spring core spring mvc即可搭建一套稳定的java web项目。本文通过spring mvc源码分析介绍它的核心实现原理。
tomcat服务器启动入口文件是web.xml,通过在其中配置相关的listener和servlet即可加载spring mvc所需数据。基于spring mvc最简单的配置如下。
- <context-param>
- <param-name>contextconfiglocationparam-name>
- <param-value>
- classpath:spring-context*.xml
- param-value>
- context-param>
- <listener>
- <listener-class>org.springframework.web.context.contextloaderlistenerlistener-class>
- listener>
- <servlet>
- <servlet-name>spring3mvcservlet-name>
- <servlet-class>org.springframework.web.servlet.dispatcherservletservlet-class>
- <init-param>
- <param-name>contextconfiglocationparam-name>
- <param-value>
- classpath:spring-mvc*.xml
- param-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>spring3mvcservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- 创建容器
contextloaderlistener基于web上下文级别的监听器在启动服务器时就创建applicationcontext并且将配置的spring bean加载到xml中。
dispatcherservlet是一个请求分发控制器,所有匹配的url都会通过该servlet分发执行,在创建servlet对象时会初始化spring mvc相关配置。
在web.xml中,我们看到基于contextloaderlistener和dispatcherservlet都可以配置spring相关的xml,值得说明的是这两种方式加载spring的applicationcontext上下文对象不是合并存储的,具体可参考http://blog.csdn.net/madun/article/details/8988860。所以个人建议,基于mvc相关的spring配置由dispatcherservlet加载,而其余的javabean都交给contextloaderlistener加载。
一、contextloaderlistener
contextloaderlistener是一个实现了servletcontextlistener接口的监听器,在启动项目时会触发contextinitialized方法(该方法主要完成applicationcontext对象的创建),在关闭项目时会触发contextdestroyed方法(该方法会执行applicationcontext清理操作)。
- public class contextloaderlistener extends contextloader implements servletcontextlistener
conextloaderlistener加载spring上下文的过程可以用下图表示,黄色区块是核心代码。
简单介绍一下上图的运行流程:
①启动项目时触发contextinitialized方法,该方法就做一件事:通过父类contextloader的initwebapplicationcontext方法创建spring上下文对象。
②initwebapplicationcontext方法做了三件事:创建webapplicationcontext;加载对应的spring文件创建里面的bean实例;将webapplicationcontext放入servletcontext(就是java web的全局变量)中。
③createwebapplicationcontext创建上下文对象,支持用户自定义的上下文对象,但必须继承自configurablewebapplicationcontext,而spring mvc默认使用configurablewebapplicationcontext作为applicationcontext(它仅仅是一个接口)的实现。
④configureandrefreshwebapplicationcontext方法用于封装applicationcontext数据并且初始化所有相关bean对象。它会从web.xml中读取名为contextconfiglocation的配置,这就是spring xml数据源设置,然后放到applicationcontext中,最后调用传说中的refresh方法执行所有java对象的创建。
⑤完成applicationcontext创建之后就是将其放入servletcontext中,注意它存储的key值常量。
- servletcontext.setattribute(webapplicationcontext.root_web_application_context_attribute, this.context);
- //常量
- public static final string root_web_application_context_attribute = webapplicationcontext.class.getname() ".root";
注:要获取 contextloader级别的ioc容器对象可以这样写:
- webapplicationcontext rootcontext = webapplicationcontextutils.getwebapplicationcontext(getservletcontext());
二、dispatcherservlet
dispatcherservlet是前端控制器设计模式的实现,提供spring web mvc的集中访问点,而且负责职责的分派,而且与spring ioc容器无缝集成,从而可以获得spring的所有好处。
要了解dispatcherservlet是如何加载容器,需要先了解它的继承关系,如下图所示:
如果在web.xml中设置了servlet的
①httpservletbean.init方法中执行initservletbean方法进行初始化操作,当然该方法在httpservletbean是空方法,所以需要子类重写。
②frameworkservlet.initservletbean子类不负众望,重写了initservletbean方法,该方法最核心的操作就是调用initwebapplicationcontext()执行上下文bean初始化。
③frameworkservlet.initwebapplicationcontext方法首先获取自己的双亲上下文(也就是contextloaderlistener初始化成功的webapplicationcontext);然后创建或者获取当前servelet的webapplicationcontext。
④无论是自己创建还是获取现有的webapplicationcontext,最终都会让servelt级别的webapplicationcontext执行configureandrefreshwebapplicationcontext()方法进行上下文容器初始化。
通过以上几步即可创建一个完整的ioc容器,而完成容器创建之后,dispatcherservlet还做了一件事:初始化servelt控制器必备对象,这个是在initwebapplicationcontext()方法中通过调用onrefresh(wac)方法实现的。而onrefresh也被重写过,如果要了解怎么初始化servlet控制器必备对象可以查看dispatcherservlet的onrefresh方法了解。
- /**
- * this implementation calls {@link #initstrategies}.
- */
- @override
- protected void onrefresh(applicationcontext context) {
- initstrategies(context);
- }
- /**
- * initialize the strategy objects that this servlet uses.
- *
may be overridden in subclasses in order to initialize further strategy objects.
- */
- protected void initstrategies(applicationcontext context) {
- initmultipartresolver(context);
- initlocaleresolver(context);
- initthemeresolver(context);
- inithandlermappings(context);
- inithandleradapters(context);
- inithandlerexceptionresolvers(context);
- initrequesttoviewnametranslator(context);
- initviewresolvers(context);
- initflashmapmanager(context);
- }
相关推荐
给大家分享一套课程,java架构师之源码分析专题springboot2.x、spring5、springmvc、mybatis源码分析,希望对大家学习有帮助。
springmvc深入理解源码分析springmvc深入理解源码分析springmvc深入理解源码分析springmvc深入理解源码分析springmvc深入理解源码分析
springmvc源码分析springmvc源码分析springmvc源码分析springmvc源码分析springmvc源码分析springmvc源码分析
主要介绍了详解springmvc容器加载源码分析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
springmvc请求流程源码分析.doc
1、手写springmvc框架及分析springmvc源码.zip1、手写springmvc框架及分析springmvc源码.zip1、手写springmvc框架及分析springmvc源码.zip1、手写springmvc框架及分析springmvc源码.zip1、手写springmvc框架及分析...
最新springmvc源码分析与实战,绝对超值,亲测,高清,完整标签,已读。
springmvc 登录注册源码,注解方式实现springmvc登录注册案例
springmvc主要流程源码解析(1).zip
springmvc.mybatis源码,下载后导入eclipse即可用,如果有什么疑问请留言,有不正确的地方还请指正
springmvc管理系统源码private string username = null; private string password = null; private string password2 = null; public void setusername(string username) { this.username = username; }
springmvc 代码搭建源码 spring mvc springmvc 代码 源码
分析springmvc源码(2).zip分析springmvc源码(2).zip分析springmvc源码(2).zip分析springmvc源码(2).zip分析springmvc源码(2).zip分析springmvc源码(2).zip分析springmvc源码(2).zip分析springmvc源码(2).zip分析...
分析springmvc源码(5).zip分析springmvc源码(5).zip分析springmvc源码(5).zip分析springmvc源码(5).zip分析springmvc源码(5).zip分析springmvc源码(5).zip分析springmvc源码(5).zip分析springmvc源码(5).zip分析...
springmvc学习指南源码
springmvc案例所有源码
springmvc项目搭建源码与步骤文档
springmvc源码,供初学者学习,有兴趣可以下载下来学习
spring springmvc maven 源码