<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaWo &#187; Java</title>
	<atom:link href="http://www.javawo.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javawo.com</link>
	<description>JavaWo关注互联网前沿的Blog.</description>
	<lastBuildDate>Thu, 13 May 2010 12:03:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>eclipse3.2+resin3.1配置</title>
		<link>http://www.javawo.com/debugging-resin-apps-in-eclipse/</link>
		<comments>http://www.javawo.com/debugging-resin-apps-in-eclipse/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 06:55:54 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[resin]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=129</guid>
		<description><![CDATA[在eclipse里添加服务器部署Web应用以后，无法正常浏览项目，需要修改resin.conf文件才能正常启动。
找到
&#60;resin:import path=&#8221;${resin.home}/conf/app-default.xml&#8221;/&#62;
改成  
&#60;resin:import path=&#8221;./conf/app-default.xml&#8221;/&#62;
或者添加环境变量
RESIN_HOME为：d:\resin-3.1.6
第二个是
 &#60;web-app-deploy path=&#8221;webapps&#8221;/&#62;的后面添加 &#60;web-app-deploy path=&#8221;deploy&#8221;/&#62;，
默认情况下，并没有这个Webapp目录，所以要添加这个目录，因为eclipse部署的WebApp都部署在这个目录下面了。
]]></description>
			<content:encoded><![CDATA[<p>在eclipse里添加服务器部署Web应用以后，无法正常浏览项目，需要修改resin.conf文件才能正常启动。</p>
<p>找到</p>
<p>&lt;resin:import path=&#8221;${resin.home}/conf/app-default.xml&#8221;/&gt;</p>
<p>改成  </p>
<p>&lt;resin:import path=&#8221;./conf/app-default.xml&#8221;/&gt;</p>
<p>或者添加环境变量</p>
<p>RESIN_HOME为：d:\resin-3.1.6</p>
<p>第二个是</p>
<p> &lt;web-app-deploy path=&#8221;webapps&#8221;/&gt;的后面添加 &lt;web-app-deploy path=&#8221;deploy&#8221;/&gt;，</p>
<p>默认情况下，并没有这个Webapp目录，所以要添加这个目录，因为eclipse部署的WebApp都部署在这个目录下面了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/debugging-resin-apps-in-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate中POJO与PO的概念</title>
		<link>http://www.javawo.com/difference-between-po-and-pojo/</link>
		<comments>http://www.javawo.com/difference-between-po-and-pojo/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 06:16:54 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Hibrenate]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[PO]]></category>
		<category><![CDATA[POJO]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=120</guid>
		<description><![CDATA[POJO = pure old java object or plain ordinary java object or what ever.
PO = persisent object 持久对象
就是说在一些Object/Relation Mapping工具中，能够做到维护数据库表记录的persisent object完全是一个符合Java Bean规范的纯Java对象，没有增加别的属性和方法。全都是这样子的：


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class User
&#123;    
	private long id;    
	private String name;   
	public void setId&#40;long id&#41; 
	&#123;   
		this.id = id;   
	&#125;     
	public [...]]]></description>
			<content:encoded><![CDATA[<p>POJO = pure old java object or plain ordinary java object or what ever.<br />
PO = persisent object 持久对象</p>
<p>就是说在一些Object/Relation Mapping工具中，能够做到维护数据库表记录的persisent object完全是一个符合Java Bean规范的纯Java对象，没有增加别的属性和方法。全都是这样子的：<br />
<span id="more-120"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> User
<span style="color: #009900;">&#123;</span>    
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>    
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>   
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>   
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>     
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>   
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">=</span>name<span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>    
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>   
		<span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>     
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		<span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>   
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>首先要区别持久对象和POJO。<br />
持久对象实际上必须对应数据库中的entity，所以和POJO有所区别。比如说POJO是由new创建，由GC回收。但是持久对象是insert数据库创建，由数据库delete删除的。基本上持久对象生命周期和数据库密切相关。另外持久对象往往只能存在一个数据库Connection之中，Connnection关闭以后，持久对象就不存在了，而POJO只要不被GC回收，总是存在的。<br />
由于存在诸多差别，因此持久对象PO(Persistent Object)在代码上肯定和POJO不同，起码PO相对于POJO会增加一些用来管理数据库entity状态的属性和方法。<br />
<strong>而ORM追求的目标就是要PO在使用上尽量和POJO一致，对于程序员来说，他们可以把PO当做POJO来用，而感觉不到PO的存在。</strong></p>
<p>Hibernate的实现方法比较先进：<br />
1、编写POJO<br />
2、编译POJO<br />
3、直接运行，在运行期，由Hibernate的CGLIB动态把POJO转换为PO。</p>
<p>由此可以看出Hibernate是在运行期把POJO的字节码转换为PO的，而JDO是在编译期转换的。一般认为JDO的方式效率会稍高，毕竟是编译期转换嘛。但是Hibernate的作者Gavin King说CGLIB的效率非常之高，运行期的PO的字节码生成速度非常之快，效率损失几乎可以忽略不计。<br />
实际上运行期生成PO的好处非常大，这样对于程序员来说，是无法接触到PO的，PO对他们来说完全透明。可以更加自由的以POJO的概念操纵PO。另外由于是运行期生成PO，所以可以支持增量编译，增量调试。而JDO则无法做到这一点。实际上已经有很多人在抱怨JDO的编译期Enhancer问题了，而据说JBossDO将采用运行期生成PO字节码，而不采用编译期生成PO字节码。</p>
<p>另外一个相关的问题是，不同的JDO产品的Enhancer生成的PO字节码可能会有所不同，可能会影响在JDO产品之间的可移植性，这一点有点类似EJB的可移植性难题。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
由这个问题另外引出一个JDO的缺陷。</p>
<p>由于JDO的PO状态管理方式，所以当你在程序里面get/set的时候，实际上不是从PO的实例中取values，而是从JDO StateManager中取出来，所以一旦PM关闭，PO就不能进行存取了。</p>
<p>在JDO中，也可以通过一些办法使得PO可以在PM外面使用，比如说定义PO是transient的，但是该PO在PM关闭后就没有PO identity了。无法进行跨PM的状态管理。</p>
<p>而Hibernate是从PO实例中取values的，所以即使Session关闭，也一样可以get/set，可以进行跨Session的状态管理。<br />
在分多层的应用中，由于持久层和业务层和web层都是分开的，此时Hibernate的PO完全可以当做一个POJO来用，也就是当做一个VO，在各层间自由传递，而不用去管Session是开还是关。如果你把这个POJO序列化的话，甚至可以用在分布式环境中。（不适合lazy loading的情况）<br />
但是JDO的PO在PM关闭后就不能再用了，所以必须在PM关闭前把PO拷贝一份VO，把VO传递给业务层和web层使用。在非分布式环境中，也可以使用ThreadLocal模式确保PM始终是打开状态，来避免每次必须进行PO到VO的拷贝操作。但是不管怎么说，这总是权宜之计，不如Hibernate的功能强。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/difference-between-po-and-pojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse和MyEclipse内存不足的解决办法</title>
		<link>http://www.javawo.com/eclipse-and-myeclipse-memory-solutions/</link>
		<comments>http://www.javawo.com/eclipse-and-myeclipse-memory-solutions/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 14:23:44 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[MyEclipse]]></category>
		<category><![CDATA[内存]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=118</guid>
		<description><![CDATA[做java项目的时候，MyEclipse老是提示内存不足，很头疼！我本本内存2G的呀。
错误提示大概如下:
MyEclipse has detected that less than 5% of the 64MB of Perm
Gen (Non-heap memory) space remains. It is strongly recommended
that you exit and restart MyEclipse with new virtual machine memory
paramters to increase this memory.   Failure to do so can result in
data loss. The recommended Eclipse memory parameters are:
eclipse.exe -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M
以下是我的MyEclipse快捷方式的参数:
&#8220;D:\Eclipse [...]]]></description>
			<content:encoded><![CDATA[<p>做java项目的时候，MyEclipse老是提示内存不足，很头疼！我本本内存2G的呀。<br />
错误提示大概如下:<br />
MyEclipse has detected that less than 5% of the 64MB of Perm<br />
Gen (Non-heap memory) space remains. It is strongly recommended<br />
that you exit and restart MyEclipse with new virtual machine memory<br />
paramters to increase this memory.   Failure to do so can result in<br />
data loss. The recommended Eclipse memory parameters are:<br />
eclipse.exe -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M</p>
<p>以下是我的MyEclipse快捷方式的参数:<br />
&#8220;D:\Eclipse 3.2.2\eclipse.exe&#8221; -product com.genuitec.myeclipse.product.ide -vmargs -Duser.language=en -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M   -Dosgi.splashLocation=&#8221;d:\MyEclipse 5.0GA\eclipse\MyEclipseSplash.bmp&#8221;</p>
<p>对于MyEclipse的话，直接修改MyEclipse在桌面的快捷方式的参数就可以啦。现在解释这些参数的意思。</p>
<p>-vmargs：说明后面是VM的参数<br />
-Xms40m：虚拟机占用系统的最小内存<br />
-Xmx256m：虚拟机占用系统的最大内存<br />
-XX:PermSize：最小堆大小。一般报内存不足时,都是说这个太小,<br />
                      堆空间剩余小于5%就会警告,建议把这个稍微设大一点,不过要视自己机器内存大小来设置<br />
-XX:MaxPermSize：最大堆大小。这个也适当大些<br />
-Xmx512M的5%为25.6M，理论上要求-Xmx的数值与-XX:MaxPermSize必须大于25.6M</p>
<p>把里面的参数改为<br />
-vmargs<br />
-Xms128M<br />
-Xmx512M<br />
-XX:PermSize=128M<br />
-XX:MaxPermSize=256M</p>
<p>所以对于我而言，我只要把我的MyEclipse5.0改-XX:PermSize=128M-XX:MaxPermSize=256M，问题解决！<br />
(个人补充一点：参数在你安装的exlipse目录下的eclipse.ini文件中)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/eclipse-and-myeclipse-memory-solutions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate对主键赋值</title>
		<link>http://www.javawo.com/hibernate-primary-key-generation/</link>
		<comments>http://www.javawo.com/hibernate-primary-key-generation/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 09:21:52 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Hibrenate]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[主键]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=113</guid>
		<description><![CDATA[今天用Hibernate操作Mysql数据库设置主键竟除了问题，主键设为uuid.String，启动服务时报错：
could not interpret id generator strategy: uuid.String
查了些资料最后得出结论：
在hibernate2.1中，主键生成策略中uuid分为uuid.hex和uuid.string，但是从hibernate3.0开始已经不再支持uuid.string
正好在复习下主键策略了。
(1) assigned
主键由外部程序负责生成，无需Hibernate参与。
(2) hilo
通过hi/lo 算法实现的主键生成机制，需要额外的数据库表保存主键生成历史状态。
(3) seqhilo
与hilo 类似，通过hi/lo 算法实现的主键生成机制，只是主键历史状态保存在Sequence中，适用于支持Sequence的数据库，如Oracle。
(4) increment
主键按数值顺序递增。此方式的实现机制为在当前应用实例中维持一个变量，以保存着当前的最大值，之后每次需要生成主键的时候将此值加1作为主键。
这种方式可能产生的问题是：如果当前有多个实例访问同一个数据库，那么由于各个实例各自维护主键状态，不同实例可能生成同样的主键，从而造成主键重复异常。因此，如果同一数据库有多个实例访问，此方式必须避免使用。
(5) identity
采用数据库提供的主键生成机制。如DB2、SQL Server、MySQL中的主键生成机制。
(6) sequence
采用数据库提供的sequence 机制生成主键。如Oralce 中的Sequence。
(7) native
由Hibernate根据底层数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式。
(8) uuid.hex
由Hibernate基于128 位唯一值产生算法生成16 进制数值（编码后以长度32 的字符串表示）作为主键。
(9) uuid.string
与uuid.hex 类似，只是生成的主键未进行编码（长度16）。在某些数据库中可能出现问题（如PostgreSQL）。
(10) foreign
使用外部表的字段作为主键。
一般而言，利用uuid.hex方式生成主键将提供最好的性能和数据库平台适
应性。
另外由于常用的数据库，如Oracle、DB2、SQLServer、MySql 等，都提供了易用的主键生成机制（Auto-Increase 字段或者Sequence）。我们可以在数据库提供的主键生成机制上，采用generator-class=native的主键生成方式。
不过值得注意的是，一些数据库提供的主键生成机制在效率上未必最佳，大量并发insert数据时可能会引起表之间的互锁。
数据库提供的主键生成机制，往往是通过在一个内部表中保存当前主键状态（如对于自增型主键而言，此内部表中就维护着当前的最大值和递增量），之后每次插入数据会读取这个最大值，然后加上递增量作为新记录的主键，之后再把这个新的最大值更新回内部表中，这样，一次Insert操作可能导致数据库内部多次表读写操作，同时伴随的还有数据的加锁解锁操作，这对性能产生了较大影响。因此，对于并发Insert要求较高的系统，推荐采用uuid.hex 作为主键生成
机制。 
]]></description>
			<content:encoded><![CDATA[<p>今天用Hibernate操作Mysql数据库设置主键竟除了问题，主键设为uuid.String，启动服务时报错：</p>
<p>could not interpret id generator strategy: uuid.String</p>
<p>查了些资料最后得出结论：</p>
<p>在hibernate2.1中，主键生成策略中uuid分为uuid.hex和uuid.string，但是从hibernate3.0开始已经不再支持uuid.string</p>
<p>正好在复习下主键策略了。</p>
<p><strong>(1) assigned</strong></p>
<p>主键由外部程序负责生成，无需Hibernate参与。</p>
<p><strong>(2) hilo</strong></p>
<p>通过hi/lo 算法实现的主键生成机制，需要额外的数据库表保存主键生成历史状态。</p>
<p><strong>(3) seqhilo</strong></p>
<p>与hilo 类似，通过hi/lo 算法实现的主键生成机制，只是主键历史状态保存在Sequence中，适用于支持Sequence的数据库，如Oracle。</p>
<p><strong>(4) increment</strong></p>
<p>主键按数值顺序递增。此方式的实现机制为在当前应用实例中维持一个变量，以保存着当前的最大值，之后每次需要生成主键的时候将此值加1作为主键。</p>
<p>这种方式可能产生的问题是：如果当前有多个实例访问同一个数据库，那么由于各个实例各自维护主键状态，不同实例可能生成同样的主键，从而造成主键重复异常。因此，如果同一数据库有多个实例访问，此方式必须避免使用。</p>
<p><strong>(5) identity</strong></p>
<p>采用数据库提供的主键生成机制。如DB2、SQL Server、MySQL中的主键生成机制。</p>
<p><strong>(6) sequence</strong></p>
<p>采用数据库提供的sequence 机制生成主键。如Oralce 中的Sequence。</p>
<p><strong>(7) native</strong></p>
<p>由Hibernate根据底层数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式。</p>
<p><strong>(8) uuid.hex</strong></p>
<p>由Hibernate基于128 位唯一值产生算法生成16 进制数值（编码后以长度32 的字符串表示）作为主键。</p>
<p><strong>(9) uuid.string</strong></p>
<p>与uuid.hex 类似，只是生成的主键未进行编码（长度16）。在某些数据库中可能出现问题（如PostgreSQL）。</p>
<p><strong>(10) foreign</strong></p>
<p>使用外部表的字段作为主键。<br />
一般而言，利用uuid.hex方式生成主键将提供最好的性能和数据库平台适<br />
应性。</p>
<p>另外由于常用的数据库，如Oracle、DB2、SQLServer、MySql 等，都提供了易用的主键生成机制（Auto-Increase 字段或者Sequence）。我们可以在数据库提供的主键生成机制上，采用generator-class=native的主键生成方式。<br />
不过值得注意的是，一些数据库提供的主键生成机制在效率上未必最佳，大量并发insert数据时可能会引起表之间的互锁。</p>
<p>数据库提供的主键生成机制，往往是通过在一个内部表中保存当前主键状态（如对于自增型主键而言，此内部表中就维护着当前的最大值和递增量），之后每次插入数据会读取这个最大值，然后加上递增量作为新记录的主键，之后再把这个新的最大值更新回内部表中，这样，一次Insert操作可能导致数据库内部多次表读写操作，同时伴随的还有数据的加锁解锁操作，这对性能产生了较大影响。因此，对于并发Insert要求较高的系统，推荐采用uuid.hex 作为主键生成<br />
机制。 </p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/hibernate-primary-key-generation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java 泛型的理解</title>
		<link>http://www.javawo.com/understanding-of-java-generics/</link>
		<comments>http://www.javawo.com/understanding-of-java-generics/#comments</comments>
		<pubDate>Sat, 16 Aug 2008 10:43:27 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[泛型]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=74</guid>
		<description><![CDATA[泛型是J2SE 5.0最重要的特性。他们让你写一个type(类或接口）和创建一个实例通过传递一个或多个引用类型。这个实例受限于只能作用于这些类型。
比如，在java 5，java.util.List 已经被泛化。当建立一个list对象时，你通过传递一个java类型建立一个List实例，此list实例只能作用于所传递的类型。这意味着如果你传递一个String ,此List实例只能拥有String对象；如果你传递一个Integer，此实例只能存贮Integer对象。除了创建参数化的类型，你还能创建参数化的函数.
     泛型的第一个好处是编译时的严格类型检查。这是集合框架最重要的特点。此外,泛型消除了绝大多数的类型转换。在JDK 5.0之前，当你使用集合框架时，你不得不进行类型转换。
     本文将教你如何操作泛型。它的第一部分是“没有泛型的日子”，先让我们回忆老版本JDK的不便。然后，举一些泛型的例子。在讨论完语法以及有界泛型的使用之后，文章最后一章将解释如何写泛型。
没有泛型的日子
    所有的java类都源自java.lang.Object,这意味着所有的JAVA对象能转换成Object。
因此，在之前的JDK的版本中，很多集合框架的函数接受一个Object参数。
所以，collections是一个能持有任何对象的多用途工具，但带来了不良的后果。
举个简单的例子，在JDK 5.0的之前版本中，类List的函数add接受一个Object参数：

1
public boolean add&#40;java.lang.Object element&#41;

所以你能传递任何类型给add。这是故意这么设计的。否则，它只能传递某种特定的对象，这样就会出现各种List类型，如，StringList, EmployeeList, AddressList等。
 add通过Object传递能带来好处,现在我们考虑get函数(返回List中的一个元素).如下是JDK 5之前版本的定义：

1
public java.lang.Object get&#40;int index&#41; throws IndexOutOfBoundsException

get返回一个Object.不幸的事情从此开始了.假如你储存了两个String对象在一个List中:

1
2
3
List stringList1 = new ArrayList&#40;&#41;;
stringList1.add&#40;&#34;Java 5&#34;&#41;;
stringList1.add&#40;&#34;with generics&#34;&#41;;

当你想从stringList1取得一个元素时,你得到了一个Object.为了操作原来的类型元素,你不得不把它转换为String。

1
String s1 = &#40;String&#41; stringList1.get&#40;0&#41;;

泛型入门
像一个函数能接受参数一样,一个泛型也能接受参数.这就是一个泛型经常被称为一个参数化类型的原因.但是不像函数用()传递参数,泛型是用&#60;&#62;传递参数的.声明一个泛型和声明一个普通类没有什么区别,只不过你把泛型的变量放在&#60;&#62;中.
比如,在JDK 5中,你可以这样声明一个java.util.List :  List&#60;E&#62; myList;
E 称为类型变量.意味着一个变量将被一个类型替代.替代类型变量的值将被当作参数或返回类型.对于List接口来说,当一个实例被创建以后,E 将被当作一个add或别的函数的参数.E 也会使get或别的参数的返回值.下面是add和get的定义:

1
2
boolean add&#60;E o&#62;
E get&#40;int index&#41;

一个泛型在声明或例示时允许你传递特定的类型变量: E.除此之外,如果E是个类，你可以传递子类；如果E是个接口，你可以传递实现接口的类；
如果你传递一个String给一个List，比如：

1
List&#60;String&#62; myList;

那么mylist的add函数将接受一个String作为他的参数，而get函数将返回一个String.因为返回了一个特定的类型，所以不用类型转化了。
根据惯例，我们使用一个唯一的大写字目表示一个类型变量。为了创建一个泛型，你需在声明时传递同样的参数列表。比如，你要想创建一个ArrayList来操作String ，你必须把String放在&#60;&#62;中。如：

1
List&#60;String&#62; myList = new ArrayList&#60;String&#62;&#40;&#41;;

再比如，java.util.Map 是这么定义的：

1
public interface Map&#60;K,V&#62;

K用来声明map键(KEY)的类型而V用来表示值(VALUE)的类型。put和values是这么定义的：

1
2
V put&#40;K [...]]]></description>
			<content:encoded><![CDATA[<p>泛型是J2SE 5.0最重要的特性。他们让你写一个type(类或接口）和创建一个实例通过传递一个或多个引用类型。这个实例受限于只能作用于这些类型。</p>
<p>比如，在java 5，java.util.List 已经被泛化。当建立一个list对象时，你通过传递一个java类型建立一个List实例，此list实例只能作用于所传递的类型。这意味着如果你传递一个String ,此List实例只能拥有String对象；如果你传递一个Integer，此实例只能存贮Integer对象。除了创建参数化的类型，你还能创建参数化的函数.<br />
     泛型的第一个好处是编译时的严格类型检查。这是集合框架最重要的特点。此外,泛型消除了绝大多数的类型转换。在JDK 5.0之前，当你使用集合框架时，你不得不进行类型转换。<span id="more-74"></span><br />
     本文将教你如何操作泛型。它的第一部分是“没有泛型的日子”，先让我们回忆老版本JDK的不便。然后，举一些泛型的例子。在讨论完语法以及有界泛型的使用之后，文章最后一章将解释如何写泛型。</p>
<p><strong>没有泛型的日子</strong><br />
    <span style="color: #ff0000;">所有的java类都源自java.lang.Object,这意味着所有的JAVA对象能转换成Object。</span><br />
因此，在之前的JDK的版本中，很多集合框架的函数接受一个Object参数。<br />
所以，collections是一个能持有任何对象的多用途工具，但带来了不良的后果。</p>
<p>举个简单的例子，在JDK 5.0的之前版本中，类List的函数add接受一个Object参数：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> add<span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">Object</span> element<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>所以你能传递任何类型给add。这是故意这么设计的。否则，它只能传递某种特定的对象，这样就会出现各种List类型，如，StringList, EmployeeList, AddressList等。</p>
<p> add通过Object传递能带来好处,现在我们考虑get函数(返回List中的一个元素).如下是JDK 5之前版本的定义：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">Object</span> get<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> index<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IndexOutOfBoundsException</span></pre></td></tr></table></div>

<p>get返回一个Object.不幸的事情从此开始了.假如你储存了两个String对象在一个List中:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span> stringList1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
stringList1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Java 5&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
stringList1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;with generics&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>当你想从stringList1取得一个元素时,你得到了一个Object.为了操作原来的类型元素,你不得不把它转换为String。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> s1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> stringList1.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>泛型入门</strong></p>
<p>像一个函数能接受参数一样,一个泛型也能接受参数.这就是一个泛型经常被称为一个参数化类型的原因.但是不像函数用()传递参数,泛型是用&lt;&gt;传递参数的.声明一个泛型和声明一个普通类没有什么区别,只不过你把泛型的变量放在&lt;&gt;中.</p>
<p>比如,在JDK 5中,你可以这样声明一个java.util.List :  List&lt;E&gt; myList;<br />
<span style="color: #ff0000;">E 称为类型变量.意味着一个变量将被一个类型替代.替代类型变量的值将被当作参数或返回类型.</span>对于List接口来说,当一个实例被创建以后,E 将被当作一个add或别的函数的参数.E 也会使get或别的参数的返回值.下面是add和get的定义:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> add<span style="color: #339933;">&lt;</span>E o<span style="color: #339933;">&gt;</span>
E get<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> index<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>一个泛型在声明或例示时允许你传递特定的类型变量: E.除此之外,如果E是个类，你可以传递子类；如果E是个接口，你可以传递实现接口的类；</p>
<p>如果你传递一个String给一个List，比如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> myList<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span style="color: #333399;">那么mylist的add函数将接受一个String作为他的参数，而get函数将返回一个String.因为返回了一个特定的类型，所以不用类型转化了。</span></p>
<p>根据惯例，我们使用一个唯一的大写字目表示一个类型变量。为了创建一个泛型，你需在声明时传递同样的参数列表。比如，你要想创建一个ArrayList来操作String ，你必须把String放在&lt;&gt;中。如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> myList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>再比如，java.util.Map 是这么定义的：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Map<span style="color: #339933;">&lt;</span>K,V<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>K用来声明map键(KEY)的类型而V用来表示值(VALUE)的类型。put和values是这么定义的：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">V put<span style="color: #009900;">&#40;</span>K key, V value<span style="color: #009900;">&#41;</span>
Collection<span style="color: #339933;">&lt;</span>V<span style="color: #339933;">&gt;</span> values<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>一个泛型不准直接的或间接的是java.lang.Throwable的子类。因为异常是在运行时抛出的,所以它不可能预言什么类型的异常将在编译时抛出.<br />
列表1的例子将比较List在JDK 1.4 和JDK1.5的不同</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GenericListTest
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// in JDK 1.4</span>
		<span style="color: #003399;">List</span> stringList1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stringList1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Java 1.0 - 5.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stringList1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;without generics&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
&nbsp;
		<span style="color: #666666; font-style: italic;">// cast to java.lang.String</span>
		<span style="color: #003399;">String</span> s1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> stringList1.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>s1.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
		<span style="color: #666666; font-style: italic;">// now with generics in JDK 5</span>
		<span style="color: #003399;">List</span> stringList2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stringList2.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Java 5.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stringList2.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;with generics&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
		<span style="color: #666666; font-style: italic;">// no need for type casting</span>
		<span style="color: #003399;">String</span> s2 <span style="color: #339933;">=</span> stringList2.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>s2.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>在列表1中，stringList2是个泛型。声明List&lt;String&gt;告诉编译器List的实例能接受一个String对象。当然，在另外的情况中，你能新建能接受各种对象的List实例。注意，当从List实例中返回成员元素时，不需要对象转化，因为他返回的了你想要的类型，也就是String.</p>
<p>最让人感兴趣的事情是，一个泛型是个类型并且能被当作一个类型变量。比如，你想你的List储存lists of Strings,你能通过把List&lt;String&gt;作为他的类型变量来声明List。比如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;&gt;</span> myListOfListsOfStrings<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>要从myList中的第一个List重新取得String，你可以这么用：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> myListOfListsOfStrings.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>下一个列表中的ListOfListsTest类示范了一个List（命名为listOfLists）接受一个String List作为参数。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ListOfListsTest 
<span style="color: #009900;">&#123;</span>  
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> listOfStrings <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		listOfStrings.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello again&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		List<span style="color: #339933;">&lt;</span>List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;&gt;</span> listOfLists <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		listOfLists.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>listOfStrings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		<span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> listOfLists.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #666666; font-style: italic;">// prints &quot;Hello again&quot;  </span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>另外，一个泛型接受一个或多个类型变量。比如，java.util.Map有两个类型变量s。第一个定义了键（key）的类型，第二个定义了值（value)的类型。下面的例子讲教我们如何使用个一个泛型Map.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MapTest
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Map</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;key1&quot;</span>, <span style="color: #0000ff;">&quot;value1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;key2&quot;</span>, <span style="color: #0000ff;">&quot;value2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> value1 <span style="color: #339933;">=</span> map.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;key1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>在这个例子中，重新得到一个key1代表的String值，我们不需要任何类型转换。</p>
<p><strong>没有参数的情况下使用泛型</strong></p>
<p>    既然在J2SE 5.0中收集类型已经泛型化，那么，原来的使用这些类型的代码将如何呢？很幸运，他们在JAVA 5中将继续工作，因为你能使用没有参数的泛型。比如，你能继续像原来一样使用List接口，正如下面的例子一样。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span> stringList1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
stringList1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Java 1.0 - 5.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
stringList1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;without generics&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> s1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> stringList1.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span style="color: #333399;">一个没有任何参数的泛型被称为原型(raw type)。</span>它意味着这些为JDK1.4或更早的版本而写的代码将继续在java 5中工作。</p>
<p>尽管如此，一个需要注意的事情是，JDK５编译器希望你使用带参数的泛型。否则，编译器将提示警告，因为他认为你可能忘了定义类型变量s。比如，编译上面的代码的时候你会看到下面这些警告，因为第一个List被认为是原型。</p>
<p>当你使用原型时，如果你不想看到这些警告，你有几个选择来达到目的：<br />
1.编译时带上参数-source 1.4<br />
2.使用@SupressWarnings(&#8220;unchecked&#8221;)注释<br />
3.更新你的代码，使用List&lt;Object&gt;. List&lt;Object&gt;的实例能接受任何类型的对象，就像是一个原型List。然而，编译器不会报错。</p>
<p><strong>使用 ? 通配符</strong><br />
  前面提过，如果你声明了一个List&lt;aType&gt;, 那么这个List对aType起作用，所以你能储存下面这些类型的对象：<br />
1.一个aType的实例<br />
2.它的子类的实例(如果aType是个类)<br />
3.实现aType接口的类实例(如果aType是个接口)<br />
但是，请注意，一个泛型本身是个JAVA类型，就像java.lang.String或java.io.File一样。传递不同的类型变量给泛型可以创建不同的JAVA类型。比如，下面例子中list1和list2引用了不同的类型对象。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> list1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> list2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>list1指向了一个类型变量s为java.lang.Objects 的List而list2指向了一个类型变量s为String 的List。所以传递一个List&lt;String&gt;给一个参数为List&lt;Object&gt;的函数将导致compile time错误。下面列表可以说明：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AllowedTypeTest 
<span style="color: #009900;">&#123;</span>  
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> doIt<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> l<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>  <span style="color: #009900;">&#125;</span>  
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> myList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		<span style="color: #666666; font-style: italic;">// 这里将产生一个错误    </span>
		doIt<span style="color: #009900;">&#40;</span>myList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>上面的代码无法编译，因为你试图传递一个错误的类型给函数doIt。doIt的参数是List&lt;Object&gt;二你传递的参数是List&lt;String&gt;。<br />
可以使用 ? 通配符解决这个难题。List&lt;?&gt; 意味着一个对任何对象起作用的List。所以，doIt可以改为：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> doIt<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;?&gt;</span> l<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>在某些情况下你会考虑使用 ? 通配符。比如，你有一个printList函数，这个函数打印一个List的所有成员，你想让这个函数对任何类型的List起作用时。否则，你只能累死累活的写很多printList的重载函数。下面的列表引用了使用 ? 通配符的printList函数。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WildCardTest 
<span style="color: #009900;">&#123;</span>   
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> printList<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;?&gt;</span> list<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> element <span style="color: #339933;">:</span> list<span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>      
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		<span style="color: #009900;">&#125;</span>  
	<span style="color: #009900;">&#125;</span>  
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> list1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		list1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		list1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		printList<span style="color: #009900;">&#40;</span>list1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
		List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> list2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		list2.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		list2.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		printList<span style="color: #009900;">&#40;</span>list2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这些代码说明了在printList函数中，List&lt;?&gt;表示各种类型的List对象。然而，请注意，在声明的时候使用 ? 通配符是不合法的，像这样：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;?&gt;</span> myList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;?&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 不合法</span></pre></td></tr></table></div>

<p>如果你想创建一个接收任何类型对象的List，你可以使用Object作为类型变量，就像这样：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> myList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>在函数中使用界限通配符<br />
</strong>在之前的章节中，你学会了通过传递不同的类型变量s来创建不同JAVA类型的泛型，但并不考虑类型变量s之间的继承关系。在很多情况下，你想一个函数有不同的List参数。比如，你有一个函数getAverage，他返回了一个List中成员的平均值。然而，如果你把List&lt;Number&gt;作为getAverage的参数，你就没法传递List&lt;Integer&gt; 或List&lt;Double&gt;参数，因为List&lt;Number&gt;和List&lt;Integer&gt; 和List&lt;Double&gt;不是同样的类型。</p>
<p>你能使用原型或使用通配符，但这样无法在编译时进行安全类型检查，因为你能传递任何类型的List，比如List&lt;String&gt;的实例。你可以使用List&lt;Number&gt;作为参数，但是你就只能传递List&lt;Number&gt;给函数。但这样就使你的函数功能减少，因为你可能更多的时候要操作List&lt;Integer&gt;或List&lt;Long&gt;，而不是List&lt;Number&gt;。</p>
<p>J2SE5.0增加了一个规则来解决了这种约束，这个规则就是允许你定义一个上界(upper bound) 类型变量.在这种方式中，你能传递一个类型或它的子类。在上面getAverage函数的例子中，你能传递一个List&lt;Number&gt;或它的子类的实例，比如List&lt;Integer&gt; or List&lt;Float&gt;。</p>
<p>使用上界规则的语法这么定义的：GenericType&lt;? extends upperBoundType&gt;. 比如，对getAverage函数的参数，你可以这么写List&lt;? extends Number&gt;. 下面例子说明了如何使用这种规则。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BoundedWildcardTest 
<span style="color: #009900;">&#123;</span>  
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">double</span> getAverage<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> Number<span style="color: #339933;">&gt;</span> numberList<span style="color: #009900;">&#41;</span>  
	<span style="color: #009900;">&#123;</span>    
		<span style="color: #000066; font-weight: bold;">double</span> total <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span>    
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Number</span> number <span style="color: #339933;">:</span> numberList<span style="color: #009900;">&#41;</span>      
			total <span style="color: #339933;">+=</span> number.<span style="color: #006633;">doubleValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		<span style="color: #000000; font-weight: bold;">return</span> total<span style="color: #339933;">/</span>numberList.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>   
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> integerList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		integerList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		integerList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		integerList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>getAverage<span style="color: #009900;">&#40;</span>integerList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 111.0    </span>
&nbsp;
		List<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span> doubleList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		doubleList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		doubleList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">33.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>getAverage<span style="color: #009900;">&#40;</span>doubleList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 18.0  </span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>由于有了上界规则，上面例子中的getAverage函数允许你传递一个List&lt;Number&gt; 或一个类型变量是任何java.lang.Number子类的List。</p>
<p><strong>下界规则</strong><br />
关键字extends定义了一个类型变量的上界。通过使用super关键字，我们可以定义一个类型变量的下界，尽管使用的情况不多。比如，如果一个函数的参数是List&lt;? super Integer&gt;，那么意味着你可以传递一个List&lt;Integer&gt;的实例或者任何java.lang.Integer的超类(superclass)。</p>
<p><strong>创建泛型</p>
<p></strong>前面的章节主要说明了如何使使用泛型，特别是集合框架中的类。现在我们开始学习如何写自己的泛型。</p>
<p>基本上，除了声明一些你想要使用的类型变量s外，一个泛型和别的类没有什么区别。这些类型变量s位于类型后面的&lt;&gt;中。比如，下面的Point就是个泛型。一个Point对象代表了一个系统中的点，它有横坐标和纵坐标。通过使Point泛型化，你能定义一个点实例的精确程度。比如，如果一个Point对象需要非常精确，你就把Double作为类型变量。否则，Integer 就够了。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Point<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> 
<span style="color: #009900;">&#123;</span>  
	T x<span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//定义泛型成员变量</span>
	T y<span style="color: #339933;">;</span>  
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#40;</span>T x, T y<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>    
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>  
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> T getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		<span style="color: #000000; font-weight: bold;">return</span> x<span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>  
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> T getY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		<span style="color: #000000; font-weight: bold;">return</span> y<span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>  
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setX<span style="color: #009900;">&#40;</span>T x<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>  
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setY<span style="color: #009900;">&#40;</span>T y<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>    
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>在这个例子中，T是Point的类型变量 。T是getX和getY的返回值类型，也是setX和setY的参数类型。此外，构造函数结合两个T参数。<br />
使用point类就像使用别的类一样。比如，下面的例子创建了两个Point对象：ponint1和point2。前者把Integer作为类型变量，而后者把Double作为类型变量。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Point<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> point1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
point1.<span style="color: #006633;">setX</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Point<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span> point2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1.3</span>, <span style="color: #cc66cc;">2.6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
point2.<span style="color: #006633;">setX</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">109.91</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>总结<br />
</strong>泛型使代码在编译时有了更严格的类型检查。特别是在集合框架中，泛型有两个作用。第一，他们增加了对集合类型在编译时的类型检查，所以集合类所能持有的类型对传递给它的参数类型起了限制作用。比如你创建了一个持有strings的java.util.List实例，那么他就将不能接受Integers或别的类型。其次，当你从一个集合中取得一个元素时，泛型消除了类型转换的必要。<br />
泛型能够在没有类型变量的情况下使用，比如，作为原型。这些措施让Java 5之前的代码能够运行在JRE 5中。但是，对新的应用程序，你最好不要使用原型，因为以后Java可能不支持他们。</p>
<p>你已经知道通过传递不同类型的类型变量给泛型可以产生不同的JAVA类型。就是说List&lt;String&gt;和List&lt;Object&gt;的类型是不同的。尽管String是java.lang.Object。但是传递一个List&lt;String&gt;给一个参数是List&lt;Object&gt;的函数会参数会产生编译错误（compile error）。函数能用 ? 通配符使其接受任何类型的参数。List&lt;?&gt; 意味着任何类型的对象。<br />
最后，你已经看到了写一个泛型和别的一般JAVA类没有什么区别。你只需要在类型名称后面的&lt;&gt;中声明一系列的类型变量s就行了。这些类型变量s就是返回值类型或者参数类型。根据惯例，一个类型变量用一个大写字母表示。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/understanding-of-java-generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring中ref local与ref bean区别</title>
		<link>http://www.javawo.com/spring-ref-local-vs-ref-bean/</link>
		<comments>http://www.javawo.com/spring-ref-local-vs-ref-bean/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 08:25:29 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[ref bean]]></category>
		<category><![CDATA[ref local]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=68</guid>
		<description><![CDATA[
&#60; bean id = &#34;userDAOProxy&#34; class = &#34;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&#34; &#62; 
        &#60; property name = &#34;transactionManager&#34; &#62; 
            &#60; ref bean = &#34;transactionManager&#34; /&#62; 
        &#60;/ property &#62; 
   [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; bean id = <span style="color: #ff0000;">&quot;userDAOProxy&quot;</span> class = <span style="color: #ff0000;">&quot;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
        <span style="color: #009900;">&lt; property name = <span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
            <span style="color: #009900;">&lt; ref bean = <span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
        <span style="color: #009900;">&lt;/ property <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
        <span style="color: #009900;">&lt; property name = <span style="color: #ff0000;">&quot;target&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
            <span style="color: #009900;">&lt; ref local = <span style="color: #ff0000;">&quot;UserDAO&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
        <span style="color: #009900;">&lt;/ property <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
    <span style="color: #009900;">&lt;/ bean <span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>1 、用 local 属性指定目标 bean 可以利用 xml 解析器的能力在同一个文件中验证 xml id 引用 . 如果在同一个文件中没有匹 配的元素 ,xml 解析器就会产生一个 error, 所以如果目标 bean 在同一个 xml 文件中 , 那么用 local 形式是最好的选择 .</p>
<p>2 、可以这么说，<br />
<strong>bean是寻找全局中的 bean; </strong><br />
<strong>local是寻找本 xml 文件中的 bean </strong></p>
<p>3 、 <ref> 提供了如下几方面的属性 :<br />
1)bean: 在当前 Spring XML 配置文件中，或者在同一 BeanFactory(ApplicationContext) 中的其他 JavaBean 中 .</p>
<p>2)local: 在当前 Spring XML 配置文件中 . 其依赖的 JavaBean 必须存在于当前 Spring XML 培植文件中 . 如果借助 于 Spring IDE, 则在编译期可以对其依赖的 JavaBean 进行验证。基于 local 方式，开发者能够使用到 XML 本身提供 的优势，而进行验证。 </p>
<p>3)parent: 用于指定其依赖的父 JavaBean 定义。 </p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/spring-ref-local-vs-ref-bean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>spring整合struts</title>
		<link>http://www.javawo.com/spring-struts-integration/</link>
		<comments>http://www.javawo.com/spring-struts-integration/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 14:10:44 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[Struts]]></category>
		<category><![CDATA[整合]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=62</guid>
		<description><![CDATA[与 Struts 相似，Spring 可以作为一个 MVC 实现。这两种框架都具有自己的优点和缺点，尽管大部分人同意 Struts 在 MVC 方面仍然是最好的。很多开发团队已经学会在时间紧迫的时候利用 Struts 作为构造高品质软件的基础。Struts 具有如此大的推动力，以至于开发团队宁愿整合 Spring 框架的特性，而不愿意转换成 Spring MVC。没必要进行转换对您来说是一个好消息。Spring 架构允许您将 Struts 作为 Web 框架连接到基于 Spring 的业务和持久层。最后的结果就是现在一切条件都具备了。
在接下来您将会了解到三种将 Struts MVC 整合到 Spring 框架的方法。我将揭示每种方法的缺陷并且对比它们的优点。 一旦您了解到所有三种方法的作用，我将会向您展示一个令人兴奋的应用程序，这个程序使用的是这三种方法中我最喜欢的一种。
使用 Spring 的 ActionSupport 类整合 Structs 
使用 Spring 的 DelegatingRequestProcessor 覆盖 Struts 的 RequestProcessor 
将 Struts Action 管理委托给 Spring 框架 
1. 使用 Spring 的 ActionSupport
手动创建一个 Spring [...]]]></description>
			<content:encoded><![CDATA[<p>与 Struts 相似，Spring 可以作为一个 MVC 实现。这两种框架都具有自己的优点和缺点，尽管大部分人同意 Struts 在 MVC 方面仍然是最好的。很多开发团队已经学会在时间紧迫的时候利用 Struts 作为构造高品质软件的基础。Struts 具有如此大的推动力，以至于开发团队宁愿整合 Spring 框架的特性，而不愿意转换成 Spring MVC。没必要进行转换对您来说是一个好消息。Spring 架构允许您将 Struts 作为 Web 框架连接到基于 Spring 的业务和持久层。最后的结果就是现在一切条件都具备了。</p>
<p>在接下来您将会了解到三种将 Struts MVC 整合到 Spring 框架的方法。我将揭示每种方法的缺陷并且对比它们的优点。 一旦您了解到所有三种方法的作用，我将会向您展示一个令人兴奋的应用程序，这个程序使用的是这三种方法中我最喜欢的一种。</p>
<p><strong>使用 Spring 的 ActionSupport 类整合 Structs </strong><br />
<strong>使用 Spring 的 DelegatingRequestProcessor 覆盖 Struts 的 RequestProcessor </strong><br />
<strong>将 Struts Action 管理委托给 Spring 框架 </strong></p>
<p><strong>1. 使用 Spring 的 ActionSupport</strong><br />
手动创建一个 Spring 环境是一种整合 Struts 和 Spring 的最直观的方式。为了使它变得更简单，Spring 提供了一些帮助。为了方便地获得 Spring 环境，org.springframework.web.struts.ActionSupport 类提供了一个 getWebApplicationContext() 方法。您所做的只是从 Spring 的 ActionSupport 而不是 Struts Action 类扩展您的动作</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">ca.nexcel.books.actions</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionError</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionErrors</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.DynaActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.ApplicationContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.struts.ActionSupport</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ca.nexcel.books.beans.Book</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ca.nexcel.books.business.BookService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchSubmit <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #009900;">&#123;</span>   <span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">public</span> ActionForward execute<span style="color: #009900;">&#40;</span>
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException <span style="color: #009900;">&#123;</span>
    DynaActionForm searchForm <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaActionForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> isbn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> searchForm.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;isbn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//the old fashion way</span>
    <span style="color: #666666; font-style: italic;">//BookService bookService = new BookServiceImpl();</span>
&nbsp;
    ApplicationContext ctx <span style="color: #339933;">=</span> 
      getWebApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
    BookService bookService <span style="color: #339933;">=</span> 
      <span style="color: #009900;">&#40;</span>BookService<span style="color: #009900;">&#41;</span> ctx.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bookService&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
&nbsp;
  <span style="color: #003399;">Book</span> book <span style="color: #339933;">=</span> bookService.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>isbn.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> book<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ActionErrors errors <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActionErrors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      errors.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ActionErrors.<span style="color: #006633;">GLOBAL_ERROR</span>,<span style="color: #000000; font-weight: bold;">new</span> ActionError
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message.notfound&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      saveErrors<span style="color: #009900;">&#40;</span>request, errors<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;failure&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
    request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;book&quot;</span>, book<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>让我们快速思考一下这里到底发生了什么。在 (1) 处，我通过从 Spring 的 ActionSupport 类而不是 Struts 的 Action 类进行扩展，创建了一个新的 Action。在 (2) 处，我使用 getWebApplicationContext() 方法获得一个 ApplicationContext。为了获得业务服务，我使用在 (2) 处获得的环境在 (3) 处查找一个 Spring bean。</p>
<p>这种技术很简单并且易于理解。不幸的是，它将 Struts 动作与 Spring 框架耦合在一起，对Struts的侵入性非常大。如果您想替换掉 Spring，那么您必须重写代码。并且，由于 Struts 动作不在 Spring 的控制之下，所以它不能获得 Spring AOP 的优势。当使用多重独立的 Spring 环境时，这种技术可能有用，但是在大多数情况下，这种方法不如另外两种方法合适。</p>
<p><strong>2. 覆盖 RequestProcessor</strong><br />
将 Spring 从 Struts 动作中分离是一个更巧妙的设计选择。分离的一种方法是使用<strong>org.springframework.web.struts.DelegatingRequestProcessor </strong>类来覆盖 Struts 的 RequestProcessor 处理程序</p>
<p>Struts配置文件：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE struts-config PUBLIC</span>
<span style="color: #00bbdd;">          &quot;-//Apache Software Foundation//DTD Struts Configuration 1.1//EN&quot;</span>
<span style="color: #00bbdd;">          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchForm&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.DynaValidatorForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;isbn&quot;</span>    <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;global-forwards</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.action.ActionForward&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;welcome&quot;</span>                <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/welcome.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchEntry&quot;</span>            <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchEntry.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchSubmit&quot;</span>           <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchSubmit.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/global-forwards<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/welcome&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/welcome.htm&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchEntry&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/search.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchSubmit&quot;</span> </span>
<span style="color: #009900;">               <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.actions.SearchSubmit&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">input</span>=<span style="color: #ff0000;">&quot;/searchEntry.do&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;success&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/detail.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;failure&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/search.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message-resources</span> <span style="color: #000066;">parameter</span>=<span style="color: #ff0000;">&quot;ApplicationResources&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controller</span> <span style="color: #000066;">processorClass</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.</span>
<span style="color: #009900;">   DelegatingRequestProcessor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> |(1)
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.ValidatorPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;pathnames&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.ContextLoaderPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;csntextConfigLocation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/beans.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>我利用了 <controller> 标记来用 DelegatingRequestProcessor 覆盖默认的 Struts RequestProcessor。下一步是在我的 Spring 配置文件中注册该动作</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; </span>
<span style="color: #00bbdd;">  &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.business.BookServiceImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;/searchSubmit&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.actions.SearchSubmit&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> |(1)
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>注意：在 (1) 处，我使用名称属性注册了一个 bean，以匹配 struts-config 动作映射名称。SearchSubmit 动作揭示了一个 JavaBean 属性，允许 Spring 在运行时填充属性</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">ca.nexcel.books.actions</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.Action</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionError</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionErrors</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.DynaActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ca.nexcel.books.beans.Book</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ca.nexcel.books.business.BookService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchSubmit <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Action</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> BookService bookService<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> BookService getBookService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> bookService<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBookService<span style="color: #009900;">&#40;</span>BookService bookService<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bookService</span> <span style="color: #339933;">=</span> bookService<span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span> 
  <span style="color: #000000; font-weight: bold;">public</span> ActionForward execute<span style="color: #009900;">&#40;</span>
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException <span style="color: #009900;">&#123;</span>
    DynaActionForm searchForm <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaActionForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> isbn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> searchForm.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;isbn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003399;">Book</span> book <span style="color: #339933;">=</span> getBookService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>isbn.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> book<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ActionErrors errors <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActionErrors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      errors.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ActionErrors.<span style="color: #006633;">GLOBAL_ERROR</span>,<span style="color: #000000; font-weight: bold;">new</span> ActionError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message.notfound&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      saveErrors<span style="color: #009900;">&#40;</span>request, errors<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;failure&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
      request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;book&quot;</span>, book<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>在 (1) 处，我创建了一个 JavaBean 属性。DelegatingRequestProcessor自动地配置这种属性。这种设计使 Struts 动作并不知道它正被 Spring 管理，并且使您能够利用 Sping 的动作管理框架的所有优点。由于您的 Struts 动作注意不到 Spring 的存在，所以您不需要重写您的 Struts 代码就可以使用其他控制反转容器来替换掉 Spring。</p>
<p>DelegatingRequestProcessor 方法的确比第一种方法好，但是仍然存在一些问题。如果您从写了Struts的 RequestProcessor，则需要手动整合 Spring 的 DelegatingRequestProcessor。添加的代码会造成维护的麻烦并且将来会降低您的应用程序的灵活性。此外，还有过一些使用一系列命令来代替 Struts RequestProcessor 的传闻。 这种改变将会对这种解决方法的使用寿命造成负面的影响。</p>
<p><strong>3. 将动作管理委托给 Spring 的 DelegatingActionProxy类</strong></p>
<p>一个更好的解决方法是将 Strut 动作管理委托给 Spring。您可以通过在 struts-config 动作映射中注册一个代理来实现。代理负责在 Spring 环境中查找 Struts 动作。由于动作在 Spring 的控制之下，所以它可以填充动作的 JavaBean 属性，并为应用诸如 Spring 的 AOP 拦截器之类的特性带来了可能。 </p>
<p>struts-config 内容：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE struts-config PUBLIC</span>
<span style="color: #00bbdd;">          &quot;-//Apache Software Foundation//DTD Struts Configuration 1.1//EN&quot;</span>
<span style="color: #00bbdd;">          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchForm&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.DynaValidatorForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;isbn&quot;</span>    <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;global-forwards</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.action.ActionForward&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;welcome&quot;</span>                <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/welcome.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchEntry&quot;</span>            <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchEntry.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchSubmit&quot;</span>           <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchSubmit.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/global-forwards<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/welcome&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/welcome.htm&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchEntry&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/search.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchSubmit&quot;</span> </span>
<span style="color: #009900;">             <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.DelegatingActionProxy&quot;</span> |<span style="color: #66cc66;">&#40;</span>1<span style="color: #66cc66;">&#41;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">input</span>=<span style="color: #ff0000;">&quot;/searchEntry.do&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;success&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/detail.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;failure&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/search.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message-resources</span> <span style="color: #000066;">parameter</span>=<span style="color: #ff0000;">&quot;ApplicationResources&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.ValidatorPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;pathnames&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.ContextLoaderPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;contextConfigLocation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/beans.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>一个典型的 struts-config.xml 文件，只有一个小小的差别。它注册 Spring 代理类的名称，而不是声明动作的类名，如（1）处所示。<strong>DelegatingActionProxy</strong> 类使用动作映射名称查找 Spring 环境中的动作。这就是我们使用 ContextLoaderPlugIn 声明的环境。</p>
<p>将一个 Struts 动作注册为一个 Spring bean 是非常直观的。我利用动作映射使用 <bean> 标记的名称属性（在这个例子中是 &#8220;/searchSubmit&#8221;）简单地创建了一个 bean。这个动作的 JavaBean 属性像任何 Spring bean 一样被填充：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; </span>
<span style="color: #00bbdd;"> &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.business.BookServiceImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;/searchSubmit&quot;</span>   </span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.actions.SearchSubmit&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>动作委托解决方法是这三种方法中最好的。Struts 动作不了解 Spring，不对代码作任何改变就可用于非 Spring 应用程序中。RequestProcessor 的改变不会影响它，并且它可以利用 Spring AOP 特性的优点。 </p>
<p>动作委托的优点不止如此。一旦让 Spring 控制您的 Struts 动作，您就可以使用 Spring 给动作补充更强的活力。例如，没有 Spring 的话，所有的 Struts 动作都必须是线程安全的。如果您设置 <bean> 标记的 singleton 属性为“false”，那么不管用何种方法，您的应用程序都将在每一个请求上有一个新生成的动作对象。您可能不需要这种特性，但是把它放在您的工具箱中也很好。您也可以利用 Spring 的生命周期方法。例如，当实例化 Struts 动作时，<bean> 标记的 init-method 属性被用于运行一个方法。类似地，在从容器中删除 bean 之前，destroy-method 属性执行一个方法。这些方法是管理昂贵对象的好办法，它们以一种与 Servlet 生命周期相同的方式进行管理。</p>
<p>另外在说下Spring容器初始化问题<br />
初始化Spring容器也有2种方式：<br />
一种是在web.xml中配置ContextLoaderListener</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>contextConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		/WEB-INF/acegi-config.xml,
		/WEB-INF/applicationContext.xml,
		/WEB-INF/action-servlet.xml,
		/WEB-INF/daoContext.xml,
		/WEB-INF/serviceContext.xml,	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		org.springframework.web.context.ContextLoaderListener
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>另一种是在struts-config.xml中配置plug-in</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.ContextLoaderPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>   
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;contextConfigLocation&quot;</span>  <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-INF/daoContext.xml,/WEB-INF/serviceContext.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>   
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>这中间存在OpenSessionInView模式的失效的问题,推荐使用第一种方法。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/spring-struts-integration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java读取网页HTML网页代码</title>
		<link>http://www.javawo.com/java-to-read-html-code/</link>
		<comments>http://www.javawo.com/java-to-read-html-code/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 13:21:25 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=56</guid>
		<description><![CDATA[利用IO流读取网页HTML网页代码

import java.net.*;   
import java.io.*;   
&#160;
public class urlconnection   
	&#123;   
		public static void main&#40;String&#91;&#93; args&#41;   
	&#123;   
	StringBuffer document = new StringBuffer&#40;&#41;;   
	try  
	&#123;   
		URL url = new URL&#40;&#34;http://www.163.com&#34;&#41;;   
		URLConnection conn = url.openConnection&#40;&#41;;   
		BufferedReader reader = new [...]]]></description>
			<content:encoded><![CDATA[<p>利用IO流读取网页HTML网页代码</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.*</span><span style="color: #339933;">;</span>   
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>   
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> urlconnection   
	<span style="color: #009900;">&#123;</span>   
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>   
	<span style="color: #009900;">&#123;</span>   
	<span style="color: #003399;">StringBuffer</span> document <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #000000; font-weight: bold;">try</span>  
	<span style="color: #009900;">&#123;</span>   
		<span style="color: #003399;">URL</span> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.163.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
		<span style="color: #003399;">URLConnection</span> conn <span style="color: #339933;">=</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
		<span style="color: #003399;">BufferedReader</span> reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>conn.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
		<span style="color: #003399;">String</span> line <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>   
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>   
		document.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
		reader.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>   
	<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">MalformedURLException</span> e<span style="color: #009900;">&#41;</span>   
	<span style="color: #009900;">&#123;</span>   
		e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>   
	<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span>   
	<span style="color: #009900;">&#123;</span>   
		e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>   
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>document.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>   
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/java-to-read-html-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
