﻿<?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-jiake</title><link>http://www.blogjava.net/jiake/</link><description /><language>zh-cn</language><lastBuildDate>Sun, 12 Apr 2026 09:39:25 GMT</lastBuildDate><pubDate>Sun, 12 Apr 2026 09:39:25 GMT</pubDate><ttl>60</ttl><item><title>Servlet容器启动后创建的对象集合（2） （转 laoer的 写的没废话）</title><link>http://www.blogjava.net/jiake/archive/2009/01/07/250235.html</link><dc:creator>jk</dc:creator><author>jk</author><pubDate>Wed, 07 Jan 2009 02:18:00 GMT</pubDate><guid>http://www.blogjava.net/jiake/archive/2009/01/07/250235.html</guid><wfw:comment>http://www.blogjava.net/jiake/comments/250235.html</wfw:comment><comments>http://www.blogjava.net/jiake/archive/2009/01/07/250235.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jiake/comments/commentRss/250235.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jiake/services/trackbacks/250235.html</trackback:ping><description><![CDATA[<p><br />
ApplicationContext是Spring的核心，Context我们通常解释为上下文环境，我想用&#8220;容器&#8221;</p>
<p>来表述它更容易理解一些，ApplicationContext则是&#8220;应用的容器&#8221;了:P，Spring把Bean放在</p>
<p>这个容器中，在需要的时候，用getBean方法取出，虽然我没有看过这一部分的源代码，但我</p>
<p>想它应该是一个类似Map的结构。 <br />
在Web应用中，我们会用到WebApplicationContext，WebApplicationContext继承自</p>
<p>ApplicationContext，先让我们看看在Web应用中，怎么初始化WebApplicationContext，在</p>
<p>web.xml中定义: <br />
&lt;context-param&gt; <br />
&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; <br />
&lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt; <br />
&lt;/context-param&gt; </p>
<p>&lt;listener&gt; <br />
&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-</p>
<p>class&gt; <br />
&lt;/listener&gt; </p>
<p>&lt;!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER <br />
&lt;servlet&gt; <br />
&lt;servlet-name&gt;context&lt;/servlet-name&gt; <br />
&lt;servlet-class&gt;org.springframework.web.context.ContextLoaderServlet&lt;/servlet-</p>
<p>class&gt; <br />
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt; <br />
&lt;/servlet&gt; <br />
--&gt; </p>
<p>可以看出，有两种方法，一个是用ContextLoaderListener这个Listerner，另一个是</p>
<p>ContextLoaderServlet这个Servlet，这两个方法都是在web应用启动的时候来初始化</p>
<p>WebApplicationContext，我个人认为Listerner要比Servlet更好一些，因为Listerner监听应</p>
<p>用的启动和结束，而Servlet得启动要稍微延迟一些，如果在这时要做一些业务的操作，启动</p>
<p>的前后顺序是有影响的。 </p>
<p>那么在ContextLoaderListener和ContextLoaderServlet中到底做了什么呢？ <br />
以ContextLoaderListener为例，我们可以看到 <br />
public void contextInitialized(ServletContextEvent event) { <br />
this.contextLoader = createContextLoader(); <br />
this.contextLoader.initWebApplicationContext(event.getServletContext()); <br />
} <br />
protected ContextLoader createContextLoader() { <br />
return new ContextLoader(); <br />
} <br />
ContextLoader是一个工具类，用来初始化WebApplicationContext，其主要方法就是</p>
<p>initWebApplicationContext，我们继续追踪initWebApplicationContext这个方法（具体代码</p>
<p>我不贴出，大家可以看Spring中的源码），我们发现，原来ContextLoader是把</p>
<p>WebApplicationContext（XmlWebApplicationContext是默认实现类）放在了ServletContext</p>
<p>中，ServletContext也是一个&#8220;容器&#8221;，也是一个类似Map的结构，而WebApplicationContext</p>
<p>在ServletContext中的KEY就是</p>
<p>WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE，我们如果要使用</p>
<p>WebApplicationContext则需要从ServletContext取出，Spring提供了一个</p>
<p>WebApplicationContextUtils类，可以方便的取出WebApplicationContext，只要把</p>
<p>ServletContext传入就可以了。 </p>
<p>上面我们介绍了WebApplicationContext在Servlet容器中初始化的原理，一般的Web应用就可</p>
<p>以轻松的使用了，但是，随着Struts的广泛应用，把Struts和Spring整个起来，是一个需要面</p>
<p>对的问题，Spring本身也提供了Struts的相关类，主要使用的有</p>
<p>org.springframework.web.struts.ActionSupport，我们只要把自己的Action继承自</p>
<p>ActionSupport，就是可以调用ActionSupport中getWebApplicationContext()的方法取出</p>
<p>WebApplicationContext，但这样一来在Action中，需要取得业务逻辑的地方都要getBean，看</p>
<p>上去不够简洁，所以Spring又提供了另一个方法，用</p>
<p>org.springframework.web.struts.ContextLoaderPlugIn，这是一个Struts的Plug，在Struts</p>
<p>启动时加载，对于Action，可以像管理Bean一样来管理，在struts-config.xml中Action的配</p>
<p>置变成类似下面的样子 <br />
&lt;action attribute="aForm" name="aForm" path="/aAction" scope="request" </p>
<p>type="org.springframework.web.struts.DelegatingActionProxy"&gt; <br />
&lt;forward name="forward" path="forward.jsp" /&gt; <br />
&lt;/action&gt; <br />
注意type变成了org.springframework.web.struts.DelegatingActionProxy，之后我们需要建</p>
<p>立action-servlet.xml这样的文件，action-servlet.xml符合Spring的spring-beans.dtd标准</p>
<p>，在里面定义类似下面的 <br />
&lt;bean name="/aAction" class="com.web.action.Aaction" singleton="false"&gt; <br />
&lt;property name="businessService"&gt; <br />
&lt;ref bean="businessService"/&gt; <br />
&lt;/property&gt; <br />
&lt;/bean&gt; </p>
<p>com.web.action.Aaction是Action的实现类，businessService是需要的业务逻辑，Spring会</p>
<p>把businessService注入到Action中，在Action中只要写businessService的get和set方法就可</p>
<p>以了，还有一点，action的bean是singleton="false"，即每次新建一个实例，这也解决了</p>
<p>Struts中Action的线程同步问题，具体过程是当用户做&#8220;/aAction&#8221;的HTTP请求（当然应该是</p>
<p>&#8220;/aAction.do&#8221;），Struts会找到这个Action的对应类</p>
<p>org.springframework.web.struts.DelegatingActionProxy，DelegatingActionProxy是个代</p>
<p>理类，它会去找action-servlet.xml文件中&#8220;/aAction&#8221;对应的真正实现类，然后把它实例化</p>
<p>，同时把需要的业务对象注入，然后执行Action的execute方法。 </p>
<p>使用了ContextLoaderPlugIn，在struts-config.xml中变成类似这样配置 <br />
&lt;plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"&gt; <br />
&lt;set-property property="contextConfigLocation" value="/WEB-</p>
<p>INF/applicationContext.xml,/WEB-INF/action-servlet.xml" /&gt; <br />
&lt;/plug-in&gt; <br />
而在web.xml中不再需要ContextLoaderListener或是ContextLoaderServlet。 </p>
<p>说到这里不知道大家会不会有这样的问题，如果使用ContextLoaderPlugIn，如果我们有些程</p>
<p>序是脱离Struts的Action环境，我们怎么处理，比如我们要自定义标记库，在标记库中，我们</p>
<p>需要调用Spring管理的业务层逻辑对象，这时候我们就很麻烦，因为只有在action中动态注入</p>
<p>业务逻辑，其他我们似乎不能取得Spring的WebApplicationContext。 </p>
<p>别急，我们还是来看一下ContextLoaderPlugIn的源码（源码不再贴出），我们可以发现，原</p>
<p>来ContextLoaderPlugIn仍然是把WebApplicationContext放在ServletContext中，只是这个</p>
<p>KEY不太一样了，这个KEY值为</p>
<p>ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX+ModuleConfig.getPrefix()（具体请查看源</p>
<p>代码），这下好了，我们知道了WebApplicationContext放在哪里，只要我们在Web应用中能够</p>
<p>取到ServletContext也就能取到WebApplicationContext了:) </p>
<p>Spring是一个很强大的框架，希望大家在使用过程中不断的深入，了解其更多的特性，我在这</p>
<p>里抛砖引玉，有什么不对的地方，请大家指出。</p>
<img src ="http://www.blogjava.net/jiake/aggbug/250235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jiake/" target="_blank">jk</a> 2009-01-07 10:18 <a href="http://www.blogjava.net/jiake/archive/2009/01/07/250235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Servlet容器启动后创建的对象集合（1）</title><link>http://www.blogjava.net/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.blogjava.net/jiake/archive/2008/12/30/249167.html</guid><wfw:comment>http://www.blogjava.net/jiake/comments/249167.html</wfw:comment><comments>http://www.blogjava.net/jiake/archive/2008/12/30/249167.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jiake/comments/commentRss/249167.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jiake/services/trackbacks/249167.html</trackback:ping><description><![CDATA[<div class="postTitle">服务器启动以后，（Servlet容器启动）创建了许多对象，如 servlet, filter, listener,spring等等 那么如何使用这些对象呢？&nbsp; 下面介绍在Servlet(或者Filter，或者Listener)中使用spring的IOC容器 <br />
默认情况下Servlet容器创建spring容器对象，注入到servletContext中，servletContext对象又是注入到session对象中，session对象又是注入到request对象中，request对象又是注入到servlet对象中，（其实不是很标准的注入，是传参数，或者对属性直接付值）。层层依赖可以得到spring容器对象。<br />
<br />
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());&nbsp;</div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;所以可以直接在ServletContext取出WebApplicationContext 对象：</p>
<p>WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);</p>
<p>事实上WebApplicationContextUtils.getWebApplicationContext方法就是使用上面的代码实现的，建议使用上面上面的静态方法&nbsp;</p>
<br />
注意：在使用webApplicationContext.getBean("ServiceName")的时候，前面强制转化要使用接口，如果使用实现类会报类型转换错误。如：<br />
LUserService&nbsp;userService ＝ (LUserService) webApplicationContext.getBean("userService"); 
<img src ="http://www.blogjava.net/jiake/aggbug/249167.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jiake/" target="_blank">jk</a> 2008-12-30 14:01 <a href="http://www.blogjava.net/jiake/archive/2008/12/30/249167.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>反射的日常应用</title><link>http://www.blogjava.net/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.blogjava.net/jiake/archive/2008/12/18/247150.html</guid><wfw:comment>http://www.blogjava.net/jiake/comments/247150.html</wfw:comment><comments>http://www.blogjava.net/jiake/archive/2008/12/18/247150.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jiake/comments/commentRss/247150.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jiake/services/trackbacks/247150.html</trackback:ping><description><![CDATA[<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;看了许多关于Comparator接口的实现和解决方法，感觉大多都不是太符合jdk的原意。<br />
&nbsp;&nbsp;&nbsp;&nbsp; Comparator接口是对Comparable接口的另一种补充。她使数据和算法分离。（在比较的时候）&nbsp;Comparable接口是数据和算法绑定，这本身并没有好和坏的分别，只是不同的角度去思考同一问题。因此从分离的角度出发，Comparator接口对比较的两个对象要求的类型更加的随意，而java&nbsp;反射机制正是对这一需求的一个合理解决方案。我们在得到比较的两个对象时，比较大小，其实就是比较他们某个属性的大小，决定返回的是-1,0,1中的一个。而属性的结果是通过方法返回的，所以我们可以通过反射得到他的方法集合，循环方法去得到希望的属性。具体的希望属性，需要用户提供比如年龄，工资，或者其他属性。这样就做到了比较并且分离算法和数据。<br />
&nbsp;&nbsp;&nbsp;&nbsp;反射的必要条件: object1,object2,field<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;代码：public int compare( Object o1, Object o2 ) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object result1 = getValue( o1 ,&nbsp;field)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object result2 = getValue( o2 , field);<br />
&nbsp;&nbsp;&nbsp;&nbsp;//<br />
&nbsp;&nbsp;if( result1 instanceof Date&nbsp; &amp;&amp; result2 instanceof Date ) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(orderFlag.equals("asc")) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ((Date)result1).compareTo((Date)result2);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(((Date)result1).compareTo((Date)result2) &lt; 0 )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(((Date)result1).compareTo((Date)result2) &gt; 0)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;//<br />
<br />
&nbsp;if( result1 instanceof String &amp;&amp;&nbsp; result2 instanceof String ) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( result1.toString().equals( result2.toString() ) ) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;//其他类型的比较！！！<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0<br />
}<br />
<br />
private Object getValue( Object obj , String fileName ) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method[] methods = &nbsp;obj.getClass().getMethods();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object value = null;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(Method method: methods){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;name = method.getName();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(name .equals("get") &amp;&amp; name .toLowerCase().indexOf(fileName )){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp; = method.invok(obj ,new Object[]{});<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}catch(Exception e){e.printStackTrace();}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<img src ="http://www.blogjava.net/jiake/aggbug/247150.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jiake/" target="_blank">jk</a> 2008-12-18 16:14 <a href="http://www.blogjava.net/jiake/archive/2008/12/18/247150.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>