??xml version="1.0" encoding="utf-8" standalone="yes"?>中文字幕欧美日本乱码一线二线,久久99国产精品久久99,精品国产一区二区三区久久久蜜臀http://www.aygfsteel.com/jiake/zh-cnFri, 20 Jun 2025 04:17:44 GMTFri, 20 Jun 2025 04:17:44 GMT60Servlet容器启动后创建的对象集合Q?Q?Q{ laoer?写的没废话)http://www.aygfsteel.com/jiake/archive/2009/01/07/250235.htmljkjkWed, 07 Jan 2009 02:18:00 GMThttp://www.aygfsteel.com/jiake/archive/2009/01/07/250235.htmlhttp://www.aygfsteel.com/jiake/comments/250235.htmlhttp://www.aygfsteel.com/jiake/archive/2009/01/07/250235.html#Feedback0http://www.aygfsteel.com/jiake/comments/commentRss/250235.htmlhttp://www.aygfsteel.com/jiake/services/trackbacks/250235.html
ApplicationContext是Spring的核心,Context我们通常解释Z下文环境Q我想用“容器”

来表q它更容易理解一些,ApplicationContext则是“应用的容?#8221;?PQSpring把Bean攑֜

q个容器中,在需要的时候,用getBeanҎ取出Q虽然我没有看过q一部分的源代码Q但?/p>

惛_应该是一个类似Map的结构?
在Web应用中,我们会用到WebApplicationContextQWebApplicationContextl承?/p>

ApplicationContextQ先让我们看看在Web应用中,怎么初始化WebApplicationContextQ在

web.xml中定?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-

class>
</listener>

<!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-

class>
<load-on-startup>1</load-on-startup>
</servlet>
-->

可以看出Q有两种ҎQ一个是用ContextLoaderListenerq个ListernerQ另一个是

ContextLoaderServletq个ServletQ这两个Ҏ都是在web应用启动的时候来初始?/p>

WebApplicationContextQ我个h认ؓListerner要比Servlet更好一些,因ؓListerner监听?/p>

用的启动和结束,而Servlet得启动要E微延迟一些,如果在这时要做一些业务的操作Q启?/p>

的前后顺序是有媄响的?

那么在ContextLoaderListener和ContextLoaderServlet中到底做了什么呢Q?
以ContextLoaderListenerZQ我们可以看?
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
ContextLoader是一个工LQ用来初始化WebApplicationContextQ其主要Ҏ是

initWebApplicationContextQ我们l追tinitWebApplicationContextq个ҎQ具体代?/p>

我不贴出Q大家可以看Spring中的源码Q,我们发现Q原来ContextLoader是把

WebApplicationContextQXmlWebApplicationContext是默认实现类Q放在了ServletContext

中,ServletContext也是一?#8220;容器”Q也是一个类似Map的结构,而WebApplicationContext

在ServletContext中的KEY是

WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTEQ我们如果要使用

WebApplicationContext则需要从ServletContext取出QSpring提供了一?/p>

WebApplicationContextUtilsc,可以方便的取出WebApplicationContextQ只要把

ServletContext传入可以了?

上面我们介绍了WebApplicationContext在Servlet容器中初始化的原理,一般的Web应用可

以轻杄使用了,但是Q随着Struts的广泛应用,把Struts和Spring整个hQ是一个需要面

对的问题QSpring本n也提供了Struts的相关类Q主要用的?/p>

org.springframework.web.struts.ActionSupportQ我们只要把自己的Actionl承?/p>

ActionSupportQ就是可以调用ActionSupport中getWebApplicationContext()的方法取?/p>

WebApplicationContextQ但q样一来在Action中,需要取得业务逻辑的地斚w要getBeanQ看

上去不够z,所以Spring又提供了另一个方法,?/p>

org.springframework.web.struts.ContextLoaderPlugInQ这是一个Struts的PlugQ在Struts

启动时加载,对于ActionQ可以像理Bean一h理Q在struts-config.xml中Action的配

|变成类g面的样子
<action attribute="aForm" name="aForm" path="/aAction" scope="request"

type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="forward" path="forward.jsp" />
</action>
注意type变成了org.springframework.web.struts.DelegatingActionProxyQ之后我们需要徏

立action-servlet.xmlq样的文Ӟaction-servlet.xmlW合Spring的spring-beans.dtd标准

Q在里面定义cM下面?
<bean name="/aAction" class="com.web.action.Aaction" singleton="false">
<property name="businessService">
<ref bean="businessService"/>
</property>
</bean>

com.web.action.Aaction是Action的实现类QbusinessService是需要的业务逻辑QSpring?/p>

把businessService注入到Action中,在Action中只要写businessService的get和setҎ可

以了Q还有一点,action的bean是singleton="false"Q即每次新徏一个实例,q也解决?/p>

Struts中Action的线E同步问题,具体q程是当用户?#8220;/aAction”的HTTPhQ当然应该是

“/aAction.do”Q,Struts会找到这个Action的对应类

org.springframework.web.struts.DelegatingActionProxyQDelegatingActionProxy是个?/p>

理类Q它会去找action-servlet.xml文g?#8220;/aAction”对应的真正实现类Q然后把它实例化

Q同时把需要的业务对象注入Q然后执行Action的executeҎ?

使用了ContextLoaderPlugInQ在struts-config.xml中变成类D样配|?
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-

INF/applicationContext.xml,/WEB-INF/action-servlet.xml" />
</plug-in>
而在web.xml中不再需要ContextLoaderListener或是ContextLoaderServlet?

说到q里不知道大家会不会有这L问题Q如果用ContextLoaderPlugInQ如果我们有些程

序是qStruts的Action环境Q我们怎么处理Q比如我们要自定义标记库Q在标记库中Q我?/p>

需要调用Spring理的业务层逻辑对象Q这时候我们就很麻烦,因ؓ只有在action中动态注?/p>

业务逻辑Q其他我们似乎不能取得Spring的WebApplicationContext?

别急,我们q是来看一下ContextLoaderPlugIn的源码(源码不再贴出Q,我们可以发现Q原

来ContextLoaderPlugIn仍然是把WebApplicationContext攑֜ServletContext中,只是q个

KEY不太一样了Q这个KEYgؓ

ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX+ModuleConfig.getPrefix()Q具体请查看?/p>

代码Q,q下好了Q我们知道了WebApplicationContext攑֜哪里Q只要我们在Web应用中能?/p>

取到ServletContext也就能取到WebApplicationContext?)

Spring是一个很强大的框Ӟ希望大家在用过E中不断的深入,了解其更多的Ҏ,我在q?/p>

里抛砖引玉,有什么不对的地方Q请大家指出?/p>

jk 2009-01-07 10:18 发表评论
]]>
Servlet容器启动后创建的对象集合Q?Q?/title><link>http://www.aygfsteel.com/jiake/archive/2008/12/30/249167.html</link><dc:creator>jk</dc:creator><author>jk</author><pubDate>Tue, 30 Dec 2008 06:01:00 GMT</pubDate><guid>http://www.aygfsteel.com/jiake/archive/2008/12/30/249167.html</guid><wfw:comment>http://www.aygfsteel.com/jiake/comments/249167.html</wfw:comment><comments>http://www.aygfsteel.com/jiake/archive/2008/12/30/249167.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/jiake/comments/commentRss/249167.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/jiake/services/trackbacks/249167.html</trackback:ping><description><![CDATA[<div id="wmqeeuq" class="postTitle">服务器启动以后,QServlet容器启动Q创Z许多对象Q如 servlet, filter, listener,spring{等 那么如何使用q些对象呢?  下面介绍在Servlet(或者FilterQ或者Listener)中用spring的IOC容器 <br /> 默认情况下Servlet容器创徏spring容器对象Q注入到servletContext中,servletContext对象又是注入到session对象中,session对象又是注入到request对象中,request对象又是注入到servlet对象中,Q其实不是很标准的注入,是传参数Q或者对属性直接付|。层层依赖可以得到spring容器对象?br /> <br /> WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); </div> <p>        所以可以直接在ServletContext取出WebApplicationContext 对象Q?/p> <p>WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);</p> <p>事实上WebApplicationContextUtils.getWebApplicationContextҎ是使用上面的代码实现的Q徏议用上面上面的静态方?nbsp;</p> <br /> 注意Q在使用webApplicationContext.getBean("ServiceName")的时候,前面强制转化要用接口,如果使用实现cM报类型{换错误。如Q?br /> LUserService userService Q?(LUserService) webApplicationContext.getBean("userService"); <img src ="http://www.aygfsteel.com/jiake/aggbug/249167.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/jiake/" target="_blank">jk</a> 2008-12-30 14:01 <a href="http://www.aygfsteel.com/jiake/archive/2008/12/30/249167.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>反射的日常应?/title><link>http://www.aygfsteel.com/jiake/archive/2008/12/18/247150.html</link><dc:creator>jk</dc:creator><author>jk</author><pubDate>Thu, 18 Dec 2008 08:14:00 GMT</pubDate><guid>http://www.aygfsteel.com/jiake/archive/2008/12/18/247150.html</guid><wfw:comment>http://www.aygfsteel.com/jiake/comments/247150.html</wfw:comment><comments>http://www.aygfsteel.com/jiake/archive/2008/12/18/247150.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/jiake/comments/commentRss/247150.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/jiake/services/trackbacks/247150.html</trackback:ping><description><![CDATA[<p>     看了许多关于Comparator接口的实现和解决ҎQ感觉大多都不是太符合jdk的原意?br />      Comparator接口是对Comparable接口的另一U补充。她使数据和法分离。(在比较的时候) Comparable接口是数据和法l定Q这本nq没有好和坏的分别,只是不同的角度去思考同一问题。因此从分离的角度出发,Comparator接口Ҏ较的两个对象要求的类型更加的随意Q而java 反射机制正是对这一需求的一个合理解x案。我们在得到比较的两个对象时Q比较大,其实是比较他们某个属性的大小Q决定返回的?1,0,1中的一个。而属性的l果是通过Ҏq回的,所以我们可以通过反射得到他的Ҏ集合Q@环方法去得到希望的属性。具体的希望属性,需要用h供比如年龄,工资Q或者其他属性。这样就做到了比较ƈ且分ȝ法和数据?br />     反射的必要条? object1,object2,field<br /> <br />     代码Qpublic int compare( Object o1, Object o2 ) {<br />         Object result1 = getValue( o1 , field)<br />         Object result2 = getValue( o2 , field);<br />     //<br />   if( result1 instanceof Date  && result2 instanceof Date ) <br />      if(orderFlag.equals("asc")) <br />       return ((Date)result1).compareTo((Date)result2);<br />      else<br />      {<br />       if(((Date)result1).compareTo((Date)result2) < 0 )<br />        return 1;<br />       if(((Date)result1).compareTo((Date)result2) > 0)<br />        return -1;<br />      }<br />     //<br /> <br />  if( result1 instanceof String &&  result2 instanceof String ) {<br />      if( result1.toString().equals( result2.toString() ) ) <br />       return 0;<br />      else <br />       return -1;<br />     }<br />     //其他cd的比较!Q!<br />       <br />     return 0<br /> }<br /> <br /> private Object getValue( Object obj , String fileName ) {<br />          Method[] methods =  obj.getClass().getMethods();<br />          Object value = null;<br />          for(Method method: methods){<br />             String name = method.getName();<br />             if(name .equals("get") && name .toLowerCase().indexOf(fileName )){<br />                 try{<br />                     value  = method.invok(obj ,new Object[]{});<br />                 }catch(Exception e){e.printStackTrace();}<br />                 break;<br />             }<br />         }<br />        return value;<br />     }</p> <img src ="http://www.aygfsteel.com/jiake/aggbug/247150.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/jiake/" target="_blank">jk</a> 2008-12-18 16:14 <a href="http://www.aygfsteel.com/jiake/archive/2008/12/18/247150.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">¹</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ֹ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Զ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ȫ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">㰲</a>| <a href="http://" target="_blank">峣</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">˳</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">۰</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ľ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank">Ͷ</a>| <a href="http://" target="_blank">ֻ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ﶫ</a>| <a href="http://" target="_blank">ն</a>| <a href="http://" target="_blank">ӻ</a>| <a href="http://" target="_blank"></a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>