﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava--随笔分类-hibernate</title><link>http://www.blogjava.net/jackstudio/category/12671.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 04:09:40 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 04:09:40 GMT</pubDate><ttl>60</ttl><item><title>spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.</title><link>http://www.blogjava.net/jackstudio/archive/2006/11/09/80060.html</link><dc:creator>jackstudio</dc:creator><author>jackstudio</author><pubDate>Thu, 09 Nov 2006 02:40:00 GMT</pubDate><guid>http://www.blogjava.net/jackstudio/archive/2006/11/09/80060.html</guid><wfw:comment>http://www.blogjava.net/jackstudio/comments/80060.html</wfw:comment><comments>http://www.blogjava.net/jackstudio/archive/2006/11/09/80060.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackstudio/comments/commentRss/80060.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackstudio/services/trackbacks/80060.html</trackback:ping><description><![CDATA[
		<p>spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.<br />看一下ContextLoaderListener的源码,这是一个ServletContextListener<br />/**<br />  * Initialize the root web application context.<br />  */<br /> public void contextInitialized(ServletContextEvent event) {<br />  this.contextLoader = createContextLoader();<br />  this.contextLoader.initWebApplicationContext(event.getServletContext());<br /> }<br /> <br />  /**<br />  * Create the ContextLoader to use. Can be overridden in subclasses.<br />  * @return the new ContextLoader<br />  */<br /> protected ContextLoader createContextLoader() {<br />  return new ContextLoader();<br /> }</p>
		<p> contextLoader的源码<br /> public WebApplicationContext initWebApplicationContext(ServletContext servletContext)<br />   throws BeansException {</p>
		<p>  long startTime = System.currentTimeMillis();<br />  if (logger.isInfoEnabled()) {<br />   logger.info("Root WebApplicationContext: initialization started");<br />  }<br />  servletContext.log("Loading Spring root WebApplicationContext");</p>
		<p>  try {<br />   // Determine parent for root web application context, if any.<br />   ApplicationContext parent = loadParentContext(servletContext);</p>
		<p>   WebApplicationContext wac = createWebApplicationContext(servletContext, parent);<br />   servletContext.setAttribute(<br />     WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);</p>
		<p>   if (logger.isInfoEnabled()) {<br />    logger.info("Using context class [" + wac.getClass().getName() +<br />      "] for root WebApplicationContext");<br />   }<br />   if (logger.isDebugEnabled()) {<br />    logger.debug("Published root WebApplicationContext [" + wac +<br />      "] as ServletContext attribute with name [" +<br />      WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");<br />   }</p>
		<p>   if (logger.isInfoEnabled()) {<br />    long elapsedTime = System.currentTimeMillis() - startTime;<br />    logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");<br />   }</p>
		<p>   return wac;<br />  }<br />  catch (RuntimeException ex) {<br />   logger.error("Context initialization failed", ex);<br />   servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);<br />   throw ex;<br />  }<br />  catch (Error err) {<br />   logger.error("Context initialization failed", err);<br />   servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);<br />   throw err;<br />  }<br /> }<br /> 注意WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,这里面放了WebApplicationContext,需要使用时从ServletContext取出<br /> 可以使用WebApplicationContextUtils得到WebApplicationContext<br /> public static WebApplicationContext getWebApplicationContext(ServletContext sc) {<br />  Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);<br />  if (attr == null) {<br />   return null;<br />  }<br />  if (attr instanceof RuntimeException) {<br />   throw (RuntimeException) attr;<br />  }<br />  if (attr instanceof Error) {<br />   throw (Error) attr;<br />  }<br />  if (!(attr instanceof WebApplicationContext)) {<br />   throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr);<br />  }<br />  return (WebApplicationContext) attr;<br /> }<br /> 关键的问题在于struts如何启动的spring的,ContextLoaderPlugIn的源码<br /> <br /> // Publish the context as a servlet context attribute.<br />  String attrName = getServletContextAttributeName();<br />  getServletContext().setAttribute(attrName, wac);<br /> <br /> public String getServletContextAttributeName() {<br />  return SERVLET_CONTEXT_PREFIX + getModulePrefix();<br /> }<br /> 不同加载的Key竟然不同,原因就是WebApplicationContext放在那里的问题,可spring调用的时候会根据WebApplicationContext里面定义的那个名字去找的,问题出在这里</p>
		<p>
				<br /> 在struts-config.xml中配置<br />    &lt;plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"&gt;<br />      &lt;set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" /&gt;<br />    &lt;/plug-in&gt;</p>
		<p>    &lt;controller&gt;<br />        &lt;set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" /&gt;<br />    &lt;/controller&gt;</p>
		<p>
				<br /> 原理是这样的,Struts虽然只能有一个ActionServlet实例,但是对于不同的子应用分别能有自己的RequestProcessor实例每个RequestProcessor实例分别对应不同的struts配置文件。<br />   子应用的ProcessorClass类必须重写一般就是继承RequestProcessor类，然后再其配置文件的controller元素中的&lt;processorClass&gt;属性中作出修改。那么当<br />  getRequestProcessor(getModuleConfig(request)).process(request,response);就能根据request选择相应的moduleconfig,再根据其&lt;processorClass&gt;属性选择相应的RequestProcessor子类来处理相应的请求了。</p>
		<p> </p>
<img src ="http://www.blogjava.net/jackstudio/aggbug/80060.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackstudio/" target="_blank">jackstudio</a> 2006-11-09 10:40 <a href="http://www.blogjava.net/jackstudio/archive/2006/11/09/80060.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>