|
该帖已经被评为新手帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-08-28 关键字: spring
xml 代码
由此,我们可知,web container把所有的请求都交给DispatcherServlet也就是 org.springframework.web.servlet.dispatcherservlet来处理了。dispatcherservlet是研究springframework mvc的关键类,在你的spring 工程中打开该类的源代码吧。 里面关键的有2句:
java 代码
java 代码
而且,每一个webapplicationcontext有一个 parent webapplicationcontext,这个parent是整个web application的context,自然也是个 beanfactory,这个parent的内容默认是从/web-inf/applicationcontext.xml文件装载,而 dispatcherservlet自己的webapplicationcontext则是从一个叫xxxx-servlet.xml的文件装载的。 xxxx就是在web.xml为dispatcherservlet定义的servlet-name. xml 代码
initwebapplicationcontext()中有这么一句: webapplicationcontext parent = webapplicationcontextutils.getwebapplicationcontext(servletcontext); 然后在createwebapplicationcontext()方法中有这么一句:wac.setparent(parent);由此可见, dispatcherservlet的webapplicationcontext的parent是在 initwebapplicationcontext()中得到,然后在createwebapplicationcontext()方法中设置的。好奇的你也许会问,这个parent 实例到底是谁生成的呢,和/web-inf/applicationcontext-hibernate.xml文件有什么关系呢.我们现在就来探讨一下这个问题。在spring工程中打开webapplicationcontextutils的 getwebapplicationcontext()方法中只有那么一句:return (webapplicationcontext) sc.getattribute (webapplicationcontext.root_web_application_context_attribute);也就是说,早就有人在web application的serlvet context里面设置好了这个属性,dispatcherservlet的 initwebapplicationcontext()只是利用webapplicationcontextutils从 servlet context把这个对象按名字检索出来而已。也许你已经按捺不住了,到底是谁设置了这个属性呢。不用急,我们打开web.xml看看,在petclinic servlet的定义之前,还有一个servlet的定义: java 代码
相信聪明的你已经从该servlet的名字和条目前面的注释猜出这个parent,也就是web application的root context是由 contextloaderservlet来设置的。没错,从contextloaderservlet的init方法中我们可以发现, contextloaderservlet生成一个contextloader,并且调用其initwebapplicationcontext()方法来设置root context。看看contextloader类,我们的答案都找到了。在其initwebapplicationcontext() 中,首先生成一个applicationcontext作为root context的parent,呵呵,在root context之上没有 context了,这个parent自然是null;而root context本身则在contextloader的 createwebapplicationcontext()方法里设置。该方法的步骤如下: 就用xmlwebapplicationcontext类来作为缺省的 context类。在我们的这个petclinic工程里面,没有使用定制的context类, 自然也就是用 xmlwebapplicationcontext类来作为context类了。 2.利用beanutils来生成一个context实例,在这里也就是一个xmlwebapplicationcontext实例。 3.设置parent属性,也就是null; 4.设置servletcontext属性,该属性还是从contextloaderservlet传过来的。 5.从web.xml中找到参数contextconfiglocation,作为配置文件的路径。我们在web.xml中定义contextconfiglocation的值是 "/web-inf/applicationcontext-hibernate.xml" 6.利用contextconfiglocation属性指出的xml配置文件中的内容刷新生成xmlwebapplicationcontext实例。 java 代码
java 代码
这个if语句会不会执行呢@也就是说frameworkservlet的私有属性contextconfiglocation是不是为空呢@也就是有没有人给frameworkservlet初始化这个属性呢@答案就在于frameworkservlet的父类httpservletbean的init() 方法中,里面有这么一句propertyvalues pvs = new servletconfigpropertyvalues (getservletconfig(), this.requiredproperties);仔细研究 servletconfigpropertyvalues()方法就知道,如果你在web.xml给dispatcherservlet定义 init-param参数contextconfiglocation的话(形式通常是诸如/web-inf/test-servlet.xml, /web-inf/myservlet.xml),父类方法init()中就会给frameworkservlet初始化这个属性. 在我们的工程并没有定义这个init-param参数,因此前面所说的if语句不会执行了。也就是frameworkservlet的 createwebapplicationcontext()方法中的wac只是初始化了属性namespace,而没有初始化 contextconfiglocation.接下来是wac.refresh();通过查看这个方法的源码(在 xmlwebapplicationcontext类中)我们就知道,在没有给xmlwebapplicationcontext初始化 contextconfiglocation属性的情况下,它会从namespace属性指出的xml配置文件中装载beandefinitions,我们的工程而言就是Dispatcher-servlet.xml文件。 至此为止,我们的 webapplicationcontext终于生成了。回到frameworkservlet的initwebapplicationcontext ()方法,我们可以发现, 这个生成的webapplicationcontext以 org.springframework.web.servlet.Frameworkservlet.CONTEXT.为key保存在servletcontext里面. 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2008-05-23
讲解的很详细,按照LZ的分析看了下源码,条理很清晰,谢谢
|
|
| 返回顶楼 | |
浏览 1250 次


