天堂的阶梯
Spring MVC framework深入分析之三(上)--执行过程
08月19日(星期五)

已经好久没有写Spring MVC深入分析了,心中不免有些愧意。也许可以用做项目、写论文、看书、暑假出去玩等理由作为借口,但是我知道这些都抵不过一个字——懒!呵呵,不过仔细想想,其实这一章的Spring MVC深入分析迟迟未提笔的重要原因是这一章太难写。有时候写抽象的东西会比写具体的东西容易,因此抽象的东西总是会停留在很高的高度,只要心里有想法就可以尽情地释放出来,所以第一章的Spring MVC深入分析我只用了一个下午多就洋洋洒洒地写完了。而写具体的东西却非常容易陷入记流水帐的泥潭,源码分析更是尤为甚之。


假如去街上找几本Linux源码分析的书,几乎每一本都是源码加注释的方式,哪怕是咱浙大出的那套经典的Linux宝书。也许这样的源码分析会有一定的帮助,尤其是象Linux那么多的代码,也许这样的流水帐式写法是比较现实的。但是当我看了JUnit A Cook's Tour 这样的文章后,我才会发现真正的源码分析不是那么写的,高手能把具体的东西抽象出来,再将这个抽象的思想进行抽丝拨茧式的分析,可以使别人一下子看到了关键,并马上明白了作者的思路和设计思想。

从具体到抽象正是高手与非高手的重要的区别,一般人的思维总是从具体到具体,最典型的就是看了别人的源码,还不知道怎么回事,就copy过来用。流水帐式的源码分析也是典型的从具体到具体的例子,他没有明白整个系统的设计思想,只是针对具体的每一行代码记下这一行干了什么东西。我不是高手,但是我决定开始装高手,学习高手的想法。假装久了,也便成了高手,呵呵!

写了一堆闲话,该言归正传啦。其实每个MVC framework的执行过程都是大同小异的,当个request过来时,它都通过一个Servlet来响应request,再根据request的路径名和配置将这个request dispatch给一个Controller执行,最后将之返回配置文件里对应的页面。在Spring MVC里,这个Servlet的名字叫DispatchServlet。稍看一下它的源码会发现这是一很简单的类。

下面是DispatchServlet的类图:

servlet类图

简单吧,这是典型的Template Method模式。每个类都会完成一些自己的本职工作,把不属于自己的工作延迟到子类来完成。这些类的子职责在下面会有分析。其实整个SpringFramework用的最多的模式就是Template MethodStrategy也挺多,呵呵),也许任何Framework用的最多的都是Template Method模式。Why?看看Expert One on One J2EE Design and Development至少Template MethodStrategy模式的分析这本书甚至比Head first Design Pattern还好。

我们先来看DispatchServlet的初始化执行过程分析吧:

我们知道每个ServletWeb服务器启动时都会有一个初始化的机会,这就是Servletinit过程,这是配置Servlet的最好机会。我们可以在这个阶段干些啥事情呢?

1、把初始化的那些init-param读到Servlet的属性里。我们知道Servletinit-param是放在ServletConfig里的,我们可以用循环去取这些属性。但是每次都这么干实在太累了,干吗不把在Servlet里增加几个property,再这些init-param直放到Servletproperty里呢?呵呵,以后那些初始参数都可以直接拿来用啦,真方便。DispatchServlet的一个祖先类叫做HttpServletBean就是专门干这个的。以后假如我们要写自己的Servlet也可以直接继承HttpServletBean这个类,这样读ServletConfig的操作都省掉了,哈哈!

2、从ServletContext里取出ApplicationContext,并扩展成自己的ApplicationContext.

ApplicationContext之谜里我们已经提到我们可以用Servlet Listner执行的机会把ApplicationContext放到ServletContext中去。但是不是直接拿这个ApplicationContext就足够了呢?no。我们先问一个简单的问题吧:在Spring MVC里我们是不是只能配置一个Servlet呢?Struts就是那么干的,所以在Struts里所有的request过来都会交给一个Servlet去处理。但是在Spring MVC里,我们却可以有好多个Servlet!它们可以处理不同类型的request,而且更重要的是它们的ApplicationContext不是相同的,它们共享了一个父ApplicationContext,也就是从ServletContext里取出来的那个,但是它们却会根据自己的配置作扩展,形成这个Servlet特有的ApplicationContext。这个子的ApplicationContext里有自己的namespace,也就是将一个叫做(假如servlet名称叫xiecc xiecc-servlet.xml的配置文件读进来,行成一个自己的ServletContext。所以这些过程全是在DispatchSevlet的一个父类FrameworkServlet里干的。

3、初始化接来要干的一件最重要的事情是初始DispatchServlet里的接口。如果用IOC容器的角度来说,其实是将ApplicationContext里定义好的接口注入到一个叫做DispatchServletbean里,只不过这个注入过程是手动的。注入的代码大致如果下(部分角色的含义和作用以后会解释):

initMultipartResolver();

initLocaleResolver();

initThemeResolver();

initHandlerMappings();

initHandlerAdapters();

initHandlerExceptionResolvers();

initViewResolvers();

每个init方法其实是属性设置的过程,因为我们可以拿到自己的ApplicationContext,所以这一切都变得很轻松啦。比如说multipartResolver,直接用getWebApplicationContext().getBean(MULTIPART_RESOLVER_BEAN_NAME);就能拿到了。

不过很多东西在配置文件里是不需要写的,比如说multipartResolver,如果在xml里取不到,Spring会初始化默认的MultipartResolver

接下来我们分析一下DispatchServlet怎么处理Request的执行流程吧:

看一下DispatchServlet的源码会发现它出奇的简单。简单的原因是类Template MethodStrategy模式,呵呵,这是我取的一个怪名字。因为DispatchServlet是不负责任何具体的操作的,它将具体的操作都delegate给了相应的接口,这是典型的Strategy模式。但是DispatchServlet里的doDispatch方法却控制了执行流程,所有的request过来,我们都会按这样的一个流程处理,这和Template Method里的由父类控制流程不谋而合。所以它仍然是Strategy模式,只不过主类还多了个控制流程的职责。

所以DispatchServlet本身的doService方法只是负责控制执行流程,而将所有具体的实现细节全都delegate给相应接口,难怪会那么简单。整个流程也异常的简单,我们甚至找不到循环跳转,所有的东西都是一直线下来的,撇开一些细节不说,剩下的就是这以几步了:

序号

步骤

具体内容

Delegate给谁干?

1

准备工作

设置theme, locale之类的东西。看看Request里是不是要上传文件,如果需要就转成MultipartRequest

2

request中取到HandlerExecutionChain

这个过程其实是提交给配置文件里定义的HandlerMapping 来做。HandlerExecutionChain是一个很有趣的东西,它包括就是我们要执行的Controller和一组interceptor,我们把它想象成一个执行单元就行啦。(细节上略有不同,下面会有分析)

HandlerMapping

3

执行HandlerExecutionChain

其实HandlerExecutionChain里已经包括了要执行的一切东西啦,我们只要把它分解开来执行就行啦。这有点象AOPWebwork的执行方法pre interceptorsàControlleràpostInterceptors

HandlerExecutionChain,它再delegate给一组interceptor和一个Controller

4

执行完的结果拿去显示,也就是所谓的render

通过ViewResolver的转换后,最后我们会delegate给一个View来完成最后的任务

ViewResolver

View

5

Render完后还有interceptor

Spring提供了三个interceptor的机会,前两个Controller方法执行前后

HandlerExecutionChain里的interceptor

文章分类: java
前篇(05-08-05): 评 Head First Design Pattern
后篇(05-08-19): Spring MVC framework深入分析之三(下)--执行过程

最新回复(129件)
主题/内容 作者/日时
Re: Spring MVC framework深入分析之三(上)--执行过程

to xiecc,但是在Spring MVC里,我们却可以有好多个Servlet!

不就一个DispatchServlet,再加上一些JavaBean处理请求,怎么说有好多Servlet

fengfeng
06-10-19 12:25
fo033.txt;2;5

fo033.txt;2;5

cNchGocLKEwNcNlvHAb
08-04-18 17:34
Extraordinarity: ,

Extraordinarity: ,

Gartman6
08-07-05 15:43
Good Work,

Good Work,

name
08-09-24 04:09
Hi There,

Hi There,

name
08-09-24 04:24
hifpclbppmLvWKDHwzv

LjqSEB

nNdkARuWrq
08-10-08 19:02
OPAokouMnroYbkplRP

eq81mK

kyuFoced
08-10-09 02:24
gcfHHqPqVKypJznNQQp

MVhZOm

WKzGDastMiKeIXg
08-10-09 04:44
fo158.txt;1;2

fo158.txt;1;2

ucksSscsUoWIdkNwk
08-11-01 18:21
As with everyone else here, we want, nay, demand more! more of your thoughts, more history and more ,,

As with everyone else here, we want, nay, demand more! more of your thoughts, more history and more ,,

Fred
08-11-20 23:43
Keep up this great resource.,

Keep up this great resource.,

Tish
08-11-26 19:43
Thanks for your great site!,

Thanks for your great site!,

Gerald
08-11-26 20:02
Hello this is nice homepage. Best work boys.,

Hello this is nice homepage. Best work boys.,

Daniel
08-11-26 20:20
There's a great deal at this pharmacy site if you want to,

There's a great deal at this pharmacy site if you want to,

Atsumi
08-11-26 20:39
Keep up this great resource.,

Keep up this great resource.,

Sarah
08-11-26 20:58
Be a superman. Be a super lover. Be with Viagra Soft Tabs!,

Be a superman. Be a super lover. Be with Viagra Soft Tabs!,

Kara
08-11-26 21:38
There's a great deal at this pharmacy site if you want to,

There's a great deal at this pharmacy site if you want to,

Dana
08-11-26 21:58
Very nice site!,

Very nice site!,

Hitomi
08-11-26 22:17
Keep up this great resource.,

Keep up this great resource.,

Joseph
08-11-26 22:36
youtube is improving well, but i have little request: ,

youtube is improving well, but i have little request: ,

Ben
08-11-27 00:21
but then I changed it back. I can see more of the,

but then I changed it back. I can see more of the,

Paul Qerch
08-11-28 12:20
of the Internet, in my opinion. ,

of the Internet, in my opinion. ,

Luetta Mcglone
08-12-02 23:36
Not about the international sites, ,

Not about the international sites, ,

Anke
08-12-07 03:59
Keep up this great resource.,

Keep up this great resource.,

Caleb
08-12-12 18:53
It would improve viewing for everyone,

It would improve viewing for everyone,

Phill Yasen
08-12-13 01:58
Limited offers!, ,

Limited offers!, ,

Gerald
09-02-11 21:32
interior for house

interior for house

Computenator
09-02-19 23:00
interior for house

interior for house

ftknecht
09-02-19 23:02
Sites: |

Sites: |

name
09-03-01 11:11
comment4, reap the wild wind, =PP, summer blonde, =-DD, peachy forum abby winters, smrfx, breaking wind icons, 1930, 225 55 17 winter tires, 545, periodic table natural state, wfxvn, table top waterfalls, 068, en plein air spring paintings, 46061, medieval winter clothing, =-PP, how to make a wind vane, 71457, home builder colorado springs, usenug, tour de palm springs, 638274, summer sandals, 435, round table pizza, >:P, horses for sale in colorado springs, juc, making wind chimes, 13085, lt1 spark table, >:-[, winter care for roses, 8-PP, spring barley, tposmd, flowers in season in summer, uwr, aluminum patio table, =-D, tallahassee summer camps, xnysc, cylindrical shaped wind turbines, 29145, wind beneath my wings lyrics, 8[[[, fanning springs, 8]], boats for sale colorado springs, cyosxy, winter nelis pear trees, 8PP, palm springs golf resorts, :-)), winter plants, 43255, home wind energy, 8[[, winter themed art, >:-PP, bardean table, 68826, summer wind lyrics, 4634, rental tables, mow, summer backyard activities, 6289, recent weddings in palm springs, bifvut, winter boots women's, :-[[, newberry springs california, qeltq, edward hopper summer evening print, rgyb, summer solstice ritual, 727624, colorado springs singles, 509852, measuring wind speed, zty, summer jobs http, erilt, key wind watches, gud, palm springs california loan officers, 8), west winds, %DDD, wind powered water pumps, =]], planting winter jasmine in containers, 7109, summer music camps tampa, :-PPP, alvin crowe palm springs, %-], spring ridge brooksville fl, xble, wind, 8PPP, jeff summers, mud, carmen electra pool table fuck, 130, wood picnic tables plans, 94892, winter haven sinkhole attorney, ysfkdw, ryan farish pacific wind, otlcq, mineral springs baptist church, 9628, roaring forties wind farm, hkyqxh, wind farm financing agreements, tmp, abby winters backstage, =-)), tornado model wind, vpw, embassy suites hot springs arkansas, edv, boys and girls summer, cvhk, aptos threads winter park, 794, heber springs lodging fishing, 75801, wind stream, 8DD, numerology tables, dzqbvk, wind for elertic power, 8-DD, summer constellations, %D, winter creeper, 03393, preparing garden for winter zone 3, 73540, spring break period for 2008, 8-OOO, photoshop tutorial for winter scene, fbhw, colorado springs newspapers, =-]]], abby winters mariko, :D, wind chill, =-)), binomial tables, rwgexg, canadian spring bulbs, yysuhx, colorado springs house flipping, %]], ohlins spring, 31097, winter pictures, %-), mission tables, %-O, west wind drive-ins, 3624, spring hill baptist church, %[[, loden spring, :-))), infant winter jacket, >:((, checker backgammon table, jlbb, wind chime music downloads, >:], coffee table cd storage, :-]]], richland springs, :-[[, halcyon hot springs, %), winter park lift ticket rates 2007, zic, who owns spring creek towers, 8-((, isotopes periodic table fe, zlava, spring hill fl, 784648, eurika springs ark, tghq, wind river vision ice ii, abjv,

comment4,

jonn2
09-04-08 16:01
comment4,

comment4,

name
09-05-31 09:01
comment1,

comment1,

name
09-05-31 09:33
Very nice site!

Very nice site!

Pharme179
09-06-03 03:32
comment5,

comment5,

name
09-06-03 17:28
comment1,

comment1,

name
09-06-04 03:16
comment1,

comment1,

name
09-06-04 03:16
comment6,

comment6,

name
09-06-11 23:45
comment3,

comment3,

name
09-06-11 23:45
doors3.txt;9;10

doors3.txt;9;10

nekieeee
09-07-06 06:54
doors2.txt;9;10

doors2.txt;9;10

nekieeee
09-07-06 10:15
doors1.txt;9;10

doors1.txt;9;10

nekieeee
09-07-06 13:20
comment6,

comment6,

name
09-07-09 21:45
2.txt;4;5

2.txt;4;5

OcffCePwqksWwaUY
09-07-31 03:18
2.txt;4;5

2.txt;4;5

aHMSufrvSofQxEy
09-07-31 04:22
2.txt;4;5

2.txt;4;5

MySvaNlF
09-07-31 05:28
hpUCCbkazCNfWsU

doors.txt;5;5

JSEaPhNxEFJmabikyS
09-08-05 21:52
cmqlfSCNYfQehol

doors.txt;5;5

kzrQTLzEi
09-08-06 00:15
gGhugkQSuZCdrirtrr

doors.txt;5;5

PBiKkNhIIo
09-08-06 01:04
KbxVLgBcJVNznxlgykm

doors.txt;5;5

lDxsTnpcewhFlocZPqB
09-08-06 02:42
abFCHVoTO

doors.txt;5;5

iUJPEATDUNulWUrEmMu
09-08-06 19:25
fGJVnDlEJwfz

doors.txt;5;5

BGzpJkNNdxGqAOthpPe
09-08-07 04:54
UuvDTjziTbdM

doors.txt;5;5

sPKLiVTORfWelXL
09-08-07 08:13
HhLgGIcL

doors.txt;5;5

TQwASxOJb
09-08-07 11:28
oKadxxAaeZhrNVaIBL

doors.txt;5;5

nopNTVIeJA
09-08-07 18:49
QcfyFMrKIlOOJFA

doors.txt;5;5

hxbMTxfyL
09-08-07 20:26
tKBjsnRkWQdQfro

doors.txt;5;5

RxhMriyZZNRcbKHW
09-08-07 21:13
LmpvyJAWvJiLaVUvo

doors.txt;5;5

fxFeTZFDAh
09-08-07 22:02
great

Yes.

avril lavigne free nude
09-08-08 04:19
rKGuxVGqHFzrP

doors.txt;5;5

TtywUFNJMdcAEGr
09-08-08 06:14
ontyime.txt;4;5

ontyime.txt;4;5

sKRYbANdQ
09-08-09 07:40
ontyime.txt;4;5

ontyime.txt;4;5

lZMcEoblyRbFiT
09-08-09 08:29
ontyime.txt;4;5

ontyime.txt;4;5

HwTIRJIfjegZUTGcZGK
09-08-09 09:18
ontyime.txt;4;5

ontyime.txt;4;5

VpEucRkb
09-08-09 10:06
ontyime.txt;4;5

ontyime.txt;4;5

NzsHzRyQOxDbVZBWq
09-08-09 11:50
ontyime.txt;4;5

ontyime.txt;4;5

JKDGhRaLlj
09-08-09 12:44
ontyime.txt;4;5

ontyime.txt;4;5

rqVpocmSBQ
09-08-09 13:33
ontyime.txt;4;5

ontyime.txt;4;5

oMMbOpYUGfX
09-08-09 14:22
ontyime.txt;4;5

ontyime.txt;4;5

mNmsFUsyWXjAGTCAdGi
09-08-09 15:10
ontyime.txt;4;5

ontyime.txt;4;5

RPnNfosHXdjahqmw
09-08-09 15:59
ontyime.txt;4;5

ontyime.txt;4;5

mlPrKPkQzOPlSIR
09-08-09 16:49
ontyime.txt;4;5

ontyime.txt;4;5

fZcyXzgRjPeuVb
09-08-09 17:41
ontyime.txt;4;5

ontyime.txt;4;5

jRIAnBgkBtlrZS
09-08-09 18:28
ontyime.txt;4;5

ontyime.txt;4;5

FCVdBaHeM
09-08-09 19:16
ontyime.txt;4;5

ontyime.txt;4;5

xImsWTmOLeSjdxCmnWP
09-08-09 20:11
ontyime.txt;4;5

ontyime.txt;4;5

EKifHxhjTzfgLl
09-08-09 21:01
ontyime.txt;4;5

ontyime.txt;4;5

uZrQgPmHqa
09-08-09 21:49
ontyime.txt;4;5

ontyime.txt;4;5

FHoJzZiKlHYllKRg
09-08-09 22:38
ontyime.txt;4;5

ontyime.txt;4;5

UpdJioyQmIBBWAWaMtj
09-08-09 23:27
Good time. ,

Good time. ,

PortoM0n
09-08-23 20:59
comment6,

comment6,

name
09-08-26 05:28
comment4,

comment4,

name
09-08-26 05:28
comment2,

comment2,

name
09-08-26 18:34
comment1,

comment1,

name
09-08-26 18:35
comment3,

comment3,

name
09-09-08 17:01
comment2,

comment2,

name
09-09-08 20:37
comment6,

comment6,

name
09-09-08 20:37
comment5,

comment5,

name
09-09-09 02:20
comment1,

comment1,

name
09-09-09 02:21
Doctor recommended doors1.txt;5

Doctor recommended
doors1.txt;5

KmWncgcvEXNyRTV
09-09-11 17:33
Sure, you can it! ,

Sure, you can it! ,

Tl1xtor
09-10-20 16:03
Really, i'm impressed. ,

Really, i'm impressed. ,

MariaOlmo
09-11-03 09:39
Great... ,

Great... ,

RaulDone
09-11-04 04:27
What your name? ,

What your name? ,

Motor114
09-11-04 23:42
What your name? ,

What your name? ,

Motor114
09-11-04 23:43
Don't go far away. ,

Don't go far away. ,

Clair5as
09-11-06 01:44
Summer. I like). ,

Summer. I like). ,

SoloJ3ss
09-11-06 20:29
Great... ,

Great... ,

Clair5as
09-11-07 14:58
Well done. ,

Well done. ,

Borkselli
09-11-09 07:47
buy drugs

Two widely used drugs

Rytebop
09-12-14 20:32
dors.txt;9;10

dors.txt;9;10

nekinn
09-12-31 03:17
dors2.txt;9;10

dors2.txt;9;10

nekinmhjh
09-12-31 06:17
dors3.txt;9;10

dors3.txt;9;10

nekjkwjkhh
09-12-31 09:12
dors4.txt;9;10

dors4.txt;9;10

nekihjjh
09-12-31 12:13
dors5.txt;9;10

dors5.txt;9;10

nekinn
09-12-31 15:10
Cialis online

I'am speachless

Cialis Ohne Rezept
10-01-02 02:26
dors2.txt;9;10

dors2.txt;9;10

nekinn
10-01-03 03:02
dors4.txt;9;10

dors4.txt;9;10

nekin
10-01-03 09:00
dors5.txt;9;10

dors5.txt;9;10

hnekhjh
10-01-03 11:58
dors6.txt;9;10

dors6.txt;9;10

nekinn
10-01-03 14:57
dors7.txt;9;10

dors7.txt;9;10

nekin
10-01-03 17:57
Hi! CDReSx

Hi! CDReSx

TFSNRgB
10-01-10 01:49
comment3,

comment3,

jonn2
10-02-21 15:12
pLokDz

pLokDz

TiJQis
10-02-22 10:45
all.txt;15;20

all.txt;15;20

tbKNWSHgVZBJrC
10-02-23 04:52
comment4,

comment4,

jonn2
10-02-23 15:11
comment2,

comment2,

jonn3
10-02-23 16:10
comment5,

comment5,

jonn3
10-02-23 18:18
Ewgwergjhvw

Ewgwergjhvw

Dleifjbe
10-02-24 14:59
Jusdbfedfwf

Jusdbfedfwf

Tggxrhey
10-02-24 15:31
Jusdbfedfwf

Jusdbfedfwf

Prlnfolm
10-02-24 16:03
Hjuasdfasef

Hjuasdfasef

Bwpzsncn
10-02-24 16:40
Jusdbfedfwf

Jusdbfedfwf

Dvpgpdtb
10-02-24 17:11
Wefwjnegjjw

Wefwjnegjjw

Ecmaojjg
10-02-24 17:42
Jusdbfedfwf

Jusdbfedfwf

Qnaasqrr
10-02-24 18:15
Lomwefgbnbv

Lomwefgbnbv

Lxgclbuz
10-02-24 18:46
Jusdbfedfwf

Jusdbfedfwf

Nbaprooc
10-02-24 19:19
Ewgwergjhvw

Ewgwergjhvw

Bwkssmkg
10-02-24 19:51
Wefwjnegjjw

Wefwjnegjjw

Rvyrknhn
10-02-24 20:53
Ewgwergjhvw

Ewgwergjhvw

Nugklpmq
10-02-24 21:25

发表评论
标题:
称呼:
内容:

引用链接
您可以按照以下步骤引用本文.本站收到您的引用通知后, 将自动链接您的文章, 以方便别人阅览 .
1. 启动您自己的博客管理页面, 并进入发表新文章的画面, 输入文章的内容. (如果您是ITPUB的博客请点这里.)
2. 复制下面虚线框里的连接字串, 把它们粘贴到您的文章中, 按照您的喜好修改一下表示文字.
3. 确认您选择了"发送引用通知"的选项.
4. 发表您的文章.
好啦, 您的文章就可以被自动链接到本站啦.

« 三月 2010 »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

国内技术blog
  • 透明思考
  • 花钱的年华

  • 国外技术blog
  • Bile blog
  • Cedric Beust
  • Cameron Purdy
  • Mike Spille
  • Ted Neward
  • Alef Arendsen

  • 技术站点
  • theserverside
  • Java视线论坛

  • 我的联系方式
  • email1
  • email2

  • 友情链接
  • 车东--搞搜索引擎的牛人
  • 除却巫山不是云--实验室的小MM
  • 奔--未来的大牛

  • 主页RSS
  • RSS 2.0
  • RSS 1.0
  • RSS 0.9


  • Creative Commons License 本站全部著作均采用CC授权. Plog 1.0 is powered by: plogworld.net.
    Itpub BLOG is provided by: itpub.net.
    This temlate(named Happy-Life's SunShine) is designed by lodge@itpub(肥猫猫).