spring DispatcherServlet建立对话分析
spring DispatcherServlet建立会话分析java 代码 if (this.contextconfiglocation ! null){wac.setconfig
spring DispatcherServlet建立会话分析
java 代码 if (this.contextconfiglocation != null){ wac.setconfiglocations(webapplicationcontextutils.parsecontextconfiglocation(this.contextconfiglocation)); 这个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里面.