﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-梦之天堂-文章分类-Java</title><link>http://www.blogjava.net/sylilzy/category/5917.html</link><description>我学故我知，我思故我在；java你我，happy你我——sylilzy</description><language>zh-cn</language><lastBuildDate>Thu, 01 Mar 2007 02:33:08 GMT</lastBuildDate><pubDate>Thu, 01 Mar 2007 02:33:08 GMT</pubDate><ttl>60</ttl><item><title>Java与数字签名</title><link>http://www.blogjava.net/sylilzy/articles/JavaAdnDigtalSign.html</link><dc:creator>sylilzy</dc:creator><author>sylilzy</author><pubDate>Thu, 06 Jul 2006 04:26:00 GMT</pubDate><guid>http://www.blogjava.net/sylilzy/articles/JavaAdnDigtalSign.html</guid><wfw:comment>http://www.blogjava.net/sylilzy/comments/56929.html</wfw:comment><comments>http://www.blogjava.net/sylilzy/articles/JavaAdnDigtalSign.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sylilzy/comments/commentRss/56929.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sylilzy/services/trackbacks/56929.html</trackback:ping><description><![CDATA[
		<p class="title">Java与数字签名</p>
		<p align="center">
				<font size="2">
						<a href="mailto:sylilzy@163.com">sylilzy@163.com</a>
				</font>
				<br />
				<font class="highLight6" size="2">版权所有,转载请注明出处</font>
		</p>
		<p>关键字：Java 数字签名 PKI Keystore 数字证书 keytool jarsigner</p>
		<p>摘要：本文介绍了数字签名的相关基础知识，并介绍了如何用java实现数字签名。</p>
		<p class="table-list">
				<strong class="headWord">数</strong>字签名作为一种电子身份的认证的手段,被普遍用于网上银行,安全网络通信等领域.数字签名是电子签名的一种特定形式.本文不对数字签名的原理作介绍,只对相关概念作一些简单的介绍,详细讲解了在java中如何对jar文件进行数字签名.</p>
		<p class="highLight2">数字签名的相关概念</p>
		<p>实际上数字签名又称作基于PKI的电子签名, PKI的核心机构是电子认证服务提供者，即通称的认证机构CA，PKI签名的核心元素是由CA签发的数字证书,数字证书就如同日常生活中的身份证一样,用来标识网上实体的身份,CA就是对网上实体进行认证的第三方机构.数字证书就是CA机构对网上实体进行认证而产生的电子证书,它是数据签名的基础技术保障.CA机构对电子证书的有效性,有效期等提供查询服务.数字证书所包含公钥用来对使用对应的私钥加密的数据信息进行验证.<br /><br />数字签名实现的具体原理是： <br />1、 将报文按双方约定的HASH算法计算得到一个固定位数的报文摘要。在数学上保证，只要改动报文中任何一位，重新计算出的报文摘要值就会与原先的值不相符。这样就保证了报文的不可更改性。(详见参考资料的"公钥密码技术原理"章节)<br />2、 将该报文摘要值用发送者的私人密钥加密，然后连同原报文和数字证书（包含公钥）一起发送给接收者而产生的报文即称数字签名。<br />3、接收方收到数字签名后，用同样的HASH算法对报文计算摘要值，然后与用发送者的公开密钥进行解密解开的报文摘要值相比较，如相等则说明报文确实来自所称的发送者。 <br />4、同时通过证书颁发机构CA确认证书的有效性即可确认发送的真实身份。<br /><br />数字证书的技术实现:<br />数字证书是公钥的载体,是PKI的核心元素.数字证书符合X.509标准,现行的PIK机制一般为又证书,即一个实体一般有两个证书,两个密码对,一个用于电子签名,一个用于加密通信.按照X.509标准,一个标准和数字证书的格式为:<br /><br />CA《A》=CA{V，SN，AI，CA，UCA，A，UA，Ap，Ta}</p>
		<p>它将包含证书颁发机构和标识及用户的标识,证书ID,有效期等信息(详见参考资料),另外还包含CA对此证书内容的进行了数字签名,以验证此证书的有效性.在验证一个数字证书的过程中,对数字证书的数字签名的验证将递归进行,只到找到双方共同可信任的CA根证书为止.</p>
		<p class="highLight2">如何获取数字证书</p>
		<p>数字证书可向专门的CA机构申请,有免费的数字证书和付费的数字证书.比如:中国数字认证网(http://www.ca365.com)、广东省电子商务认证中心(http://www.cnca.net/)就可申请到有效期为一年的免费数字证书.注意申请的证书是以该CA的证书作为申请的个人证书的根证书,所以如果想要申请的证书有效,需要下载并安装相应CA的根证书.另外,其它一些工具也可生成个人使用的数字证书,如微软的makecert(下载地址http://www.microsoft.com/downloads/details.aspx?familyid=2b742795-d0f0-4a66-b27f-22a95fcd3425&amp;displaylang=en))，JDK中包含的keytool工具(以下会详细介绍如何使用)都可生成没有经过认证的数字证书. </p>
		<p class="highLight2">Java如何实现数字签名</p>
		<p>JDK有部分工具和API提供了对数字签名的支持:keytool,jarsigner,policytool等.</p>
		<p>KeyTool工具可生成,导入,导出数字证书,支持的证书的格式为当前流行的</p>
		<p class="highLight4">生成证书:keytool -genkey -alias mykey -keystore mystore</p>
		<p class="highLight4">导入证书:keytool -import -alias abc -file ABCCA.cer -keystore mystore</p>
		<p class="highLight4">导出证书: keytool -export -alias mykey -file MJ.cer -keystore mystore</p>
		<p>对jar文件进行签名使用的工具是jarsigner:</p>
		<p>格式如下:<span class="highLight4">jarsigner -keystore keystoreFile jarfile alias</span></p>
		<p>例如:<span class="highLight4">jarsigner -keystore sylilzyKeystore.dat test.jar sylilzy</span></p>
		<p>验证一个jar文件如下:</p>
		<p>jarsigner -verify jarfile</p>
		<p>以下步骤演示了一个完整的由获取数字证书到签名jar文件,然后访问该jar文件时显示签名信息的全过程:</p>
		<p>上面提到,数字证书可由以下方式生成,第一,去http://www.ca365.com申请一个免费的数字证书,下载后为test.der</p>
		<p>也可用keytool -genkey -alias mykey -keystore mystore生成,运行此命令后,提示相关信息,并填写完毕,如下:</p>
		<p class="table-info">E:\temp&gt;keytool -genkey -alias mykey -keystore mystore<br />输入keystore密码： 111111<br />您的名字与姓氏是什么？<br />[Unknown]： shi<br />您的组织单位名称是什么？<br />[Unknown]： eshore<br />您的组织名称是什么？<br />[Unknown]： de<br />您所在的城市或区域名称是什么？<br />[Unknown]： gz<br />您所在的州或省份名称是什么？<br />[Unknown]： gd<br />该单位的两字母国家代码是什么<br />[Unknown]： cn<br />CN=shi, OU=eshore, O=de, L=gz, ST=gd, C=cn 正确吗？<br />[否]： y<br />输入&lt;mykey&gt;的主密码<br />（如果和 keystore 密码相同，按回车）：</p>
		<p>到此,我们则生成了一个名为mystore的keystore文件,该文件中保存有别名为mykey的数字证书.输入以下命令即可查看此数字证书:</p>
		<p class="table-info">E:\temp&gt;keytool -list -alias mykey -keystore mystore<br />输入keystore密码： 111111<br />mykey, 2006-7-5, keyEntry,<br />认证指纹 (MD5)： A1:A9:A7:C6:D8:E5:E5:20:EA:80:59:AF:C2:65:B4:17<br /></p>
		<p>或者带上-v参数显示详细信息</p>
		<p class="table-info">E:\temp&gt;keytool -list -alias mykey -keystore mystore -v<br />输入keystore密码： 111111<br />别名名称： mykey<br />创建日期： 2006-7-5<br />输入类型：KeyEntry<br />认证链长度： 1<br />认证 [1]:<br />Owner: CN=shi, OU=eshore, O=de, L=gz, ST=gd, C=cn<br />发照者： CN=shi, OU=eshore, O=de, L=gz, ST=gd, C=cn<br />序号： 44ab3fb8<br />有效期间： Wed Jul 05 12:27:36 CST 2006 至： Tue Oct 03 12:27:36 CST 2006<br />认证指纹：<br />MD5： A1:A9:A7:C6:D8:E5:E5:20:EA:80:59:AF:C2:65:B4:17<br />SHA1： AC:8D:AA:9D:A7:62:48:70:ED:F4:28:4C:DC:DE:56:CE:41:FE:52:C9</p>
		<p>以上为创建的一个证书,我们可以将它从keystore文件中导出:</p>
		<p class="table-info">E:\temp&gt;keytool -export -keystore mystore -alias mykey -file my.cer<br />输入keystore密码： 111111<br />保存在文件中的认证 &lt;my.cer&gt;</p>
		<p>我们也可将前面从网上申请的电子证书导入keystore文件:</p>
		<p class="table-info">E:\temp&gt;keytool -import -alias sily -file test.der -keystore mystore<br />输入keystore密码： 111111<br />Owner: CN=施祖阳, OU=develop, O=eshore, L=广州, ST=广东, C=CN<br />发照者： CN=CA365 Free Root Certificate, O=CA365, L=Beijing, ST=Beijing, C=CN<br />序号： 653e1a63bb003fb3<br />有效期间： Tue Jul 04 12:02:25 CST 2006 至： Wed Jul 04 12:02:25 CST 2007<br />认证指纹：<br />MD5： 4A:AA:63:2A:8A:E2:8D:76:4B:2B:73:E9:F3:03:CD:6F<br />SHA1： 02:B3:9E:D0:7E:BB:9E:C4:B3:B0:79:19:FA:89:B6:93:DB:0F:4A:88<br />信任这个认证？ [否]： y<br />认证已添加至keystore中</p>
		<p>现在查看mystore中已经存了两个证书:</p>
		<p class="table-info">E:\temp&gt;keytool -list -keystore mystore<br />输入keystore密码： 111111<br />Keystore 类型： jks<br />Keystore 提供者： SUN<br />您的 keystore 包含 2 输入<br />sily, 2006-7-5, trustedCertEntry,<br />认证指纹 (MD5)： 4A:AA:63:2A:8A:E2:8D:76:4B:2B:73:E9:F3:03:CD:6F<br />mykey, 2006-7-5, keyEntry,<br />认证指纹 (MD5)： A1:A9:A7:C6:D8:E5:E5:20:EA:80:59:AF:C2:65:B4:17</p>
		<p>现在,我们开始利用前面生成的数字证书给jar文件签名.由于applet在浏览器中运行时的权限是受限的,不能对本地文件进行读写,但是如果applet所在的jar文件如果是经过数字签名,并且用户信任该签名,那此applet则可对本地文件进行读写,下面给出一个写本地文件的applet:</p>
		<p>
		</p>
		<div style="BORDER-RIGHT: black 1px dotted; BORDER-TOP: black 1px dotted; FONT-SIZE: 10pt; BORDER-LEFT: black 1px dotted; BORDER-BOTTOM: black 1px dotted; FONT-FAMILY: Courier New">
				<!-- Code creation by HtmlSave Eclipse Plug-in (C) 2005 Morten Moeller / eclipse.moelleryoung.com -->
				<span style="COLOR: #3f7f5f"> * Created on 2004-7-16 <br /></span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">package</span>
				<span> </span>
				<span style="COLOR: #000000">com.applet;</span>
				<span>
						<br />
						<br />
				</span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">import</span>
				<span> </span>
				<span style="COLOR: #000000">java.applet.Applet;</span>
				<span>
						<br />
						<br />
				</span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">public</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">class</span>
				<span> </span>
				<span style="COLOR: #000000">TestSecurity</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">extends</span>
				<span> </span>
				<span style="COLOR: #000000">Applet</span>
				<span> </span>
				<span style="COLOR: #000000">{</span>
				<span>
						<br />    </span>
				<span style="COLOR: #3f5fbf">/**</span>
				<span>
						<br />     </span>
				<span style="COLOR: #3f5fbf">*</span>
				<span>  <br />     </span>
				<span style="COLOR: #3f5fbf">*/</span>
				<span>
						<br />    </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">private</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">static</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">final</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">long</span>
				<span> </span>
				<span style="COLOR: #0000c0; FONT-STYLE: italic">serialVersionUID</span>
				<span> </span>
				<span style="COLOR: #000000">=</span>
				<span> </span>
				<span style="COLOR: #000000">1L;</span>
				<span>
						<br />
						<br />    </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">public</span>
				<span> </span>
				<span style="COLOR: #000000">TestSecurity()</span>
				<span> </span>
				<span style="COLOR: #000000">{</span>
				<span>
						<br />        </span>
				<span style="COLOR: #000000">System.</span>
				<span style="COLOR: #0000c0; FONT-STYLE: italic">out</span>
				<span style="COLOR: #000000">.println(</span>
				<span style="COLOR: #2a00ff">"this is good! "</span>
				<span style="COLOR: #000000">);</span>
				<span>
						<br />    </span>
				<span style="COLOR: #000000">}</span>
				<span>
						<br />
						<br />    </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">public</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">void</span>
				<span> </span>
				<span style="COLOR: #000000">init()</span>
				<span> </span>
				<span style="COLOR: #000000">{</span>
				<span>
						<br />        </span>
				<span style="COLOR: #000000">JButton</span>
				<span> </span>
				<span style="COLOR: #000000">button</span>
				<span> </span>
				<span style="COLOR: #000000">=</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">new</span>
				<span> </span>
				<span style="COLOR: #000000">JButton(</span>
				<span style="COLOR: #2a00ff">"Create a file"</span>
				<span style="COLOR: #000000">);</span>
				<span>
						<br />        </span>
				<span style="COLOR: #000000">button.addActionListener(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">new</span>
				<span> </span>
				<span style="COLOR: #000000">ActionListener()</span>
				<span> </span>
				<span style="COLOR: #000000">{</span>
				<span>
						<br />            </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">public</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">void</span>
				<span> </span>
				<span style="COLOR: #000000">actionPerformed(ActionEvent</span>
				<span> </span>
				<span style="COLOR: #000000">evt)</span>
				<span> </span>
				<span style="COLOR: #000000">{</span>
				<span>
						<br />                </span>
				<span style="COLOR: #000000">File</span>
				<span> </span>
				<span style="COLOR: #000000">file</span>
				<span> </span>
				<span style="COLOR: #000000">=</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">new</span>
				<span> </span>
				<span style="COLOR: #000000">File(</span>
				<span style="COLOR: #2a00ff">"c:\\a.txt"</span>
				<span style="COLOR: #000000">);</span>
				<span>
						<br />                </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">try</span>
				<span> </span>
				<span style="COLOR: #000000">{</span>
				<span>
						<br />                    </span>
				<span style="COLOR: #000000">file.createNewFile();</span>
				<span>
						<br />                    </span>
				<span style="COLOR: #000000">JOptionPane.</span>
				<span style="COLOR: #000000; FONT-STYLE: italic">showMessageDialog</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">null</span>
				<span style="COLOR: #000000">,</span>
				<span> </span>
				<span style="COLOR: #2a00ff">" 成功创建文件c:\\a.txt"</span>
				<span style="COLOR: #000000">,</span>
				<span> </span>
				<span style="COLOR: #2a00ff">"消息"</span>
				<span style="COLOR: #000000">,</span>
				<span>
						<br />                            </span>
				<span style="COLOR: #000000">JOptionPane.</span>
				<span style="COLOR: #0000c0; FONT-STYLE: italic">INFORMATION_MESSAGE</span>
				<span style="COLOR: #000000">);</span>
				<span>
						<br />                </span>
				<span style="COLOR: #000000">}</span>
				<span> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">catch</span>
				<span> </span>
				<span style="COLOR: #000000">(Exception</span>
				<span> </span>
				<span style="COLOR: #000000">ex)</span>
				<span> </span>
				<span style="COLOR: #000000">{</span>
				<span>
						<br />                    </span>
				<span style="COLOR: #000000">JOptionPane.</span>
				<span style="COLOR: #000000; FONT-STYLE: italic">showMessageDialog</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #7f0055">null</span>
				<span style="COLOR: #000000">,</span>
				<span> </span>
				<span style="COLOR: #000000">ex.getMessage(),</span>
				<span> </span>
				<span style="COLOR: #2a00ff">"错误"</span>
				<span style="COLOR: #000000">,</span>
				<span> </span>
				<span style="COLOR: #000000">JOptionPane.</span>
				<span style="COLOR: #0000c0; FONT-STYLE: italic">ERROR_MESSAGE</span>
				<span style="COLOR: #000000">);</span>
				<span>
						<br />                </span>
				<span style="COLOR: #000000">}</span>
				<span>
						<br />            </span>
				<span style="COLOR: #000000">}</span>
				<span>
						<br />        </span>
				<span style="COLOR: #000000">});</span>
				<span>
						<br />        </span>
				<span style="COLOR: #000000">add(button);</span>
				<span>
						<br />    </span>
				<span style="COLOR: #000000">}</span>
				<span>
						<br />
				</span>
				<span style="COLOR: #000000">}</span>
		</div>
		<span style="COLOR: #000000">
				<p>
				</p>
				<p>将此文件编译后生成在com\applet生成两个class文件,用jar打包:</p>
				<p class="table-info">E:\sylilzy\documents\project\tjava\classes&gt;jar -cvf test.jar com<br />标明清单(manifest)<br />增加：com/(读入= 0) (写出= 0)(存储了 0%)<br />...</p>
				<p>会发现在当前目录下会生成一个"test.jar"文件,将此文件copy到与mystore同一个目录,准备对该文件进行电子签名.</p>
				<p class="table-info">E:\temp&gt;jarsigner -keystore mystore test.jar mykey<br />输入密钥库的口令短语： 111111<br />警告： 签名者证书将在六个月内过期。</p>
				<p>则test.jar已经被签名,编码一个简单的html文件对其进行访问,html如下:</p>
				<pre>
						<font size="2">
Created with Colorer-take5 Library. Type 'html'

<span style="COLOR: #a65700">&lt;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">html</span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">head</span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">meta</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">http-equiv</span><span style="COLOR: #808030">=</span><span style="COLOR: #0000e6">"Content-Type"</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">content</span><span style="COLOR: #808030">=</span><span style="COLOR: #0000e6">"text/html; charset=GB2312"</span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">title</span><span style="COLOR: #a65700">&gt;</span>
			HTML Test Page
		<span style="COLOR: #a65700">&lt;/</span><span style="FONT-WEIGHT: bold; COLOR: #800000">title</span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;/</span><span style="FONT-WEIGHT: bold; COLOR: #800000">head</span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">body</span><span style="COLOR: #a65700">&gt;</span>
		test<span style="COLOR: #008c00">.</span>Applet1 will appear below in a Java enabled browser<span style="COLOR: #008c00">.</span><span style="COLOR: #a65700">&lt;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">br</span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">applet</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">codebase</span><span style="COLOR: #274796"></span><span style="COLOR: #808030">=</span><span style="COLOR: #274796"></span><span style="COLOR: #0000e6">"."</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">code</span><span style="COLOR: #274796"></span><span style="COLOR: #808030">=</span><span style="COLOR: #274796"></span><span style="COLOR: #0000e6">"com.applet.TestSecurity"</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">name</span><span style="COLOR: #274796"></span><span style="COLOR: #808030">=</span><span style="COLOR: #274796"></span><span style="COLOR: #0000e6">"TestApplet"</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">width</span><span style="COLOR: #274796"></span><span style="COLOR: #808030">=</span><span style="COLOR: #274796"></span><span style="COLOR: #0000e6">"400"</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">height</span><span style="COLOR: #274796"></span><span style="COLOR: #808030">=</span><span style="COLOR: #274796"></span><span style="COLOR: #0000e6">"300"</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">hspace</span><span style="COLOR: #274796"></span><span style="COLOR: #808030">=</span><span style="COLOR: #274796"></span><span style="COLOR: #0000e6">"0"</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">vspace</span><span style="COLOR: #274796"></span><span style="COLOR: #808030">=</span><span style="COLOR: #274796"></span><span style="COLOR: #0000e6">"0"</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">align</span><span style="COLOR: #274796"></span><span style="COLOR: #808030">=</span><span style="COLOR: #274796"></span><span style="COLOR: #0000e6">"middle"</span><span style="COLOR: #274796"></span><span style="COLOR: #074726">archive</span><span style="COLOR: #808030">=</span><span style="COLOR: #0000e6">"test.jar"</span><span style="COLOR: #274796"></span><span style="COLOR: #274796"></span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;/</span><span style="FONT-WEIGHT: bold; COLOR: #800000">applet</span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;/</span><span style="FONT-WEIGHT: bold; COLOR: #800000">body</span><span style="COLOR: #a65700">&gt;</span><span style="COLOR: #a65700">&lt;/</span><span style="FONT-WEIGHT: bold; COLOR: #800000">html</span><span style="COLOR: #a65700">&gt;</span></font>
				</pre>
				<p>
				</p>
				<p>将以上html文件与test.jar文件放在同一目录,用IE打开,则可看到IE弹出安全警告: The application's digital signature is invalid.Do you want to run the application? <br /><br />用户可查看test.jar的签名信息,如果选择取消息,然后点击create a file 按钮,则提示:access denied(java.io.FilePermission c:\a.txt write) </p>
				<p>如果在IE弹出安全警告选择"始终信任此发行者的内容",然后点击运行,再点击create a file 按钮,则提示:成功创建文件c:\a.txt</p>
				<p>上面给test.jar签名的数字证书是未经验证的,现在我们使用从网上申请的经过验证的数字证书给test.jar签名,看会出现什么结果:</p>
				<p class="table-info">E:\temp&gt;jarsigner -keystore mystore test.jar sily<br />输入密钥库的口令短语： 111111<br />jarsigner： 找不到 sily 的证书链。sily 必须引用包含专用密钥和相应的公共密钥证书链的有效密钥库密钥条目。</p>
				<p>这是因为sily的证书链不存在导致的,我们需要导入sily的证书链才可形成完整的sily的证书链,可以通过keytool -certreq生成证书链的PKCS#10格式的请求,然后再导入请求的PKCS#7格式的回复即可,由于名为sily的key是从cer文件导入的,它在keystore中是以trusted certificate entries类型存储的,没有私钥,所以不能生成证书链的请求.我们可以将前面名为mykey的key导出证书请求,或者导出mykey的证书,然后将此证书安装至"受信任的根证书颁发机构",然后访问用此证书签名过的文件,则IE将不再弹出安全警告窗口.</p>
		</span>
		<hr />
		<span style="COLOR: #000000">
				<p>
				</p>
				<h3>公告</h3>
				<p>
						<br />参考资料：<br /><a href="http://www.qqread.com/net-saft/z532114081.html">http://www.qqread.com/net-saft/z532114081.html</a> 电子签名的技术实现<br /><a href="http://www.qqread.com/java/w292768600.html">http://www.qqread.com/java/w292768600.html</a> JAVA对数字证书的常用操作<br /><a href="http://java.sun.com/j2se/1.5.0/docs/index.html">http://java.sun.com/j2se/1.5.0/docs/index.html</a> JDK 5.0 Documentation </p>
				<p>
				</p>
				<p>
				</p>
				<fieldset>
						<legend>作者简介</legend>施祖阳，网名sylilzy,1979年生。<br />2002年起从事软件开发工作，主要研究JAVA、Linux及相关技术。<br />你可通过<a href="mailto:sylilzy@163.com">sylilzy@163.com</a>与作者联系。 </fieldset>
				<p>
				</p>
		</span>
<img src ="http://www.blogjava.net/sylilzy/aggbug/56929.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sylilzy/" target="_blank">sylilzy</a> 2006-07-06 12:26 <a href="http://www.blogjava.net/sylilzy/articles/JavaAdnDigtalSign.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring的事务处理详解:调用一个方法前的事务处理过程(源代码分析)</title><link>http://www.blogjava.net/sylilzy/articles/52822.html</link><dc:creator>sylilzy</dc:creator><author>sylilzy</author><pubDate>Wed, 14 Jun 2006 10:34:00 GMT</pubDate><guid>http://www.blogjava.net/sylilzy/articles/52822.html</guid><wfw:comment>http://www.blogjava.net/sylilzy/comments/52822.html</wfw:comment><comments>http://www.blogjava.net/sylilzy/articles/52822.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sylilzy/comments/commentRss/52822.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sylilzy/services/trackbacks/52822.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: spring的事务处理详解:调用一个方法前的事务处理过程(源代码分析)																																				 																																																												sylilzy@163.com																													...&nbsp;&nbsp;<a href='http://www.blogjava.net/sylilzy/articles/52822.html'>阅读全文</a><img src ="http://www.blogjava.net/sylilzy/aggbug/52822.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sylilzy/" target="_blank">sylilzy</a> 2006-06-14 18:34 <a href="http://www.blogjava.net/sylilzy/articles/52822.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring的事务处理详解:调用一个方法后的事务处理过程</title><link>http://www.blogjava.net/sylilzy/articles/52823.html</link><dc:creator>sylilzy</dc:creator><author>sylilzy</author><pubDate>Wed, 14 Jun 2006 10:34:00 GMT</pubDate><guid>http://www.blogjava.net/sylilzy/articles/52823.html</guid><wfw:comment>http://www.blogjava.net/sylilzy/comments/52823.html</wfw:comment><comments>http://www.blogjava.net/sylilzy/articles/52823.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sylilzy/comments/commentRss/52823.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sylilzy/services/trackbacks/52823.html</trackback:ping><description><![CDATA[
		<div>
				<div>
						<font color="#000080">
								<strong>
										<div align="center">
												<font size="2">spring的事务处理详解:调用一个方法后的事务处理过程</font>
										</div>
										<div align="center">
												<font size="2">
												</font> </div>
										<div align="center">
												<font style="BACKGROUND-COLOR: #000000" color="#00ff00">
														<a href="file:///C:/DOCUME~1/SHIZUY~1/LOCALS~1/Temp/WebCatcher/mailto:sylilzy@163.com">
																<font style="BACKGROUND-COLOR: #ffffff" size="2">
																		<strong>sylilzy@163.com</strong>
																</font>
														</a>
														<div class="diaryTitle" align="center">
																<font style="BACKGROUND-COLOR: #0000ff" color="#ffffff" size="2">版权所有,转载请注明出处</font>
														</div>
												</font>
										</div>
								</strong>
						</font>
				</div>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<font color="#000080" size="2">
				<strong>
				</strong>
		</font>
		<div>
				<font color="#000080" size="2">
						<strong>spring调用一个方法后的事务处理过程:</strong>
				</font>
		</div>
		<div>
				<font color="#000080" size="2">
						<strong>请</strong>
				</font>
				<font color="#000080" size="2">
						<strong>参照TransactionInterceptor的invoke方法:</strong>
				</font>
		</div>
		<div>
				<font color="#000080" size="2">
						<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
								<span style="COLOR: #008080"> 1</span>
								<img id="Codehighlighter1_4_188_Open_Image" onclick="this.style.display='none'; Codehighlighter1_4_188_Open_Text.style.display='none'; Codehighlighter1_4_188_Closed_Image.style.display='inline'; Codehighlighter1_4_188_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								<img id="Codehighlighter1_4_188_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_4_188_Closed_Text.style.display='none'; Codehighlighter1_4_188_Open_Image.style.display='inline'; Codehighlighter1_4_188_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								<span style="COLOR: #0000ff">try</span>
								<span style="COLOR: #000000"> </span>
								<span id="Codehighlighter1_4_188_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.blogjava.net/images/dot.gif" />
								</span>
								<span id="Codehighlighter1_4_188_Open_Text">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080"> 2</span>
										<span style="COLOR: #000000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> This is an around advice.<br /></span>
										<span style="COLOR: #008080"> 3</span>
										<span style="COLOR: #008000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> Invoke the next interceptor in the chain.<br /></span>
										<span style="COLOR: #008080"> 4</span>
										<span style="COLOR: #008000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> This will normally result in a target object being invoked.</span>
										<span style="COLOR: #008000">
												<br />
										</span>
										<span style="COLOR: #008080"> 5</span>
										<span style="COLOR: #008000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   retVal </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> invocation.proceed();<br /></span>
										<span style="COLOR: #008080"> 6</span>
										<span style="COLOR: #000000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />  }</span>
								</span>
								<span style="COLOR: #000000">
										<br />
								</span>
								<span style="COLOR: #008080"> 7</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_213_312_Open_Image" onclick="this.style.display='none'; Codehighlighter1_213_312_Open_Text.style.display='none'; Codehighlighter1_213_312_Closed_Image.style.display='inline'; Codehighlighter1_213_312_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_213_312_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_213_312_Closed_Text.style.display='none'; Codehighlighter1_213_312_Open_Image.style.display='inline'; Codehighlighter1_213_312_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />  </span>
								<span style="COLOR: #0000ff">catch</span>
								<span style="COLOR: #000000"> (Throwable ex) </span>
								<span id="Codehighlighter1_213_312_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.blogjava.net/images/dot.gif" />
								</span>
								<span id="Codehighlighter1_213_312_Open_Text">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080"> 8</span>
										<span style="COLOR: #000000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> target invocation exception</span>
										<span style="COLOR: #008000">
												<br />
										</span>
										<span style="COLOR: #008080"> 9</span>
										<span style="COLOR: #008000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   doCloseTransactionAfterThrowing(txInfo, ex);<br /></span>
										<span style="COLOR: #008080">10</span>
										<span style="COLOR: #000000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">throw</span>
										<span style="COLOR: #000000"> ex;<br /></span>
										<span style="COLOR: #008080">11</span>
										<span style="COLOR: #000000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />  }</span>
								</span>
								<span style="COLOR: #000000">
										<br />
								</span>
								<span style="COLOR: #008080">12</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_324_350_Open_Image" onclick="this.style.display='none'; Codehighlighter1_324_350_Open_Text.style.display='none'; Codehighlighter1_324_350_Closed_Image.style.display='inline'; Codehighlighter1_324_350_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_324_350_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_324_350_Closed_Text.style.display='none'; Codehighlighter1_324_350_Open_Image.style.display='inline'; Codehighlighter1_324_350_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />  </span>
								<span style="COLOR: #0000ff">finally</span>
								<span style="COLOR: #000000"> </span>
								<span id="Codehighlighter1_324_350_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.blogjava.net/images/dot.gif" />
								</span>
								<span id="Codehighlighter1_324_350_Open_Text">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">13</span>
										<span style="COLOR: #000000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   doFinally(txInfo);<br /></span>
										<span style="COLOR: #008080">14</span>
										<span style="COLOR: #000000">
												<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />  }</span>
								</span>
								<span style="COLOR: #000000">
										<br />
								</span>
								<span style="COLOR: #008080">15</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  doCommitTransactionAfterReturning(txInfo);<br /></span>
								<span style="COLOR: #008080">16</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> retVal;<br /></span>
								<span style="COLOR: #008080">17</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /> }</span>
						</div>
				</font>
		</div>
		<div>
				<font color="#000080" size="2">
				</font> </div>
		<div>
				<strong>
						<font color="#000080" size="2">调用目标方法时没有抛出异常时会调用:</font>
				</strong>
		</div>
		<div>
				<font color="#000080" size="2">doCommitTransactionAfterReturning(txInfo);</font>
		</div>
		<div>
				<font color="#000080" size="2">然后直接提交事务</font>
		</div>
		<div>
				<font color="#000080" size="2">
				</font> </div>
		<div>
				<font color="#000080" size="2">如果抛异常则检查是否需要回滚事务(doCloseTransactionAfterThrowing方法会根据在spring中设备的Transaction Attribute),否则提交事务.</font>
		</div>
		<div>
				<font color="#000080">
						<div>
								<font size="2">
										<hr />
								</font>
						</div>
						<div>
								<font size="2">参考资料:</font>
						</div>
						<div>
								<font size="2">1.Spring Reference Manual:http://static.springframework.org/spring/docs/1.2.x/reference/index.html</font>
						</div>
						<div>
								<font size="2">2.Spring API doc:http://static.springframework.org/spring/docs/1.2.x/api/index.html</font>
						</div>
						<div>
								<font size="2">3.Spring 的源代码</font>
						</div>
						<div>
								<font size="2">
										<hr />
								</font>
						</div>
						<div>
								<div>
										<font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">作者简介：</font>
								</div>
								<div>
										<font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">    施祖阳，网名sylilzy,1979年生。</font>
								</div>
								<div>
										<font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">    2002年起从事软件开发工作，主要研究为JAVA、Linux及相关技术。</font>
								</div>
								<div>
										<font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">    你可通过</font>
										<a href="file:///C:/DOCUME~1/SHIZY~1.GDC/LOCALS~1/Temp/WebCatcher/mailto:sylilzy@163.com">
												<font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">sylilzy@163.com</font>
										</a>
										<font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">与作者联系。</font>
								</div>
						</div>
				</font>
		</div>
<img src ="http://www.blogjava.net/sylilzy/aggbug/52823.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sylilzy/" target="_blank">sylilzy</a> 2006-06-14 18:34 <a href="http://www.blogjava.net/sylilzy/articles/52823.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring的事务处理详解:事务创建</title><link>http://www.blogjava.net/sylilzy/articles/52810.html</link><dc:creator>sylilzy</dc:creator><author>sylilzy</author><pubDate>Wed, 14 Jun 2006 10:01:00 GMT</pubDate><guid>http://www.blogjava.net/sylilzy/articles/52810.html</guid><wfw:comment>http://www.blogjava.net/sylilzy/comments/52810.html</wfw:comment><comments>http://www.blogjava.net/sylilzy/articles/52810.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sylilzy/comments/commentRss/52810.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sylilzy/services/trackbacks/52810.html</trackback:ping><description><![CDATA[
		<div align="center">
				<font size="3">spring的事务处理详解:事务创建</font>
		</div>
		<div align="center"> </div>
		<div align="center">
				<font style="BACKGROUND-COLOR: #000000" color="#00ff00">sylilzy@163.com</font>
		</div>
		<div> </div>
		<div>
				<font color="#000080">
						<div>
								<font color="#000080">在配置spring的事务处理时,无论使用TransactionProxyFactoryBean,还是使用BeanNameAutoProxyCreator</font>
						</div>
						<div>
								<font color="#000080">spring的事务处理都是主要由TransactionInterceptor来拦截完成,此类扩展自org.aopalliance.intercept.MethodInterceptor,要查看spring的事务处理过程,首先要了解</font>
						</div>
						<div>
								<font color="#000080">TransactionInterceptor类的执行过程</font>:</div>
				</font>
		</div>
		<div> </div>
		<div> </div>
		<div> </div>
		<div>1.事务拦截器拦截调用方法:invoke();</div>
		<div>2.调用TransactionAspectSupport的createTransactionIfNecessary,</div>
		<div> </div>
		<div> </div>
		<div>createTransactionIfNecessary中:</div>
		<div>首先创建TransactionInfo对象,然后</div>
		<div>如果被调用的方法设置了事务属性(不管是什么属性,只要设置了),输出日志:</div>
		<div> </div>
		<div>TransactionInterceptor 221 - Getting transaction for ...</div>
		<div> </div>
		<div>并调用TransactionManager.getTransaction方法,</div>
		<div> </div>
		<div>如果被调用的方法设置未设备事务,输出:Don't need to create transaction for ...</div>
		<div> </div>
		<div>调用返回后然后将事务绑定到当前线程.createTransactionIfNecessary方法返回.</div>
		<div> </div>
		<div>3.调用目标类的方法,并依次完成其它拦截器的调用</div>
		<div>4.如果在上一步操作中有异常抛出,则会处理异常,处理过程:根据配置决定是提交还是回滚事务</div>
		<div>5.如无异常,调用doFinally()将上一个事务(旧事务)设置为当前事务</div>
		<div>4.<font color="#ff0000">调用doCommitTransactionAfterReturning提交事务,此为最重要的一步,与事务相关的操作在此实际生效.</font></div>
		<div> </div>
		<div>
				<font color="#000080">调用TransactionManager.getTransaction过程 :</font>
		</div>
		<div>1.调用doGetTransaction()查找事务(此对象并不代码一个事务已经存在),返回的对象中包含事务的相关信息:如事务是否开始等.此对象将在以后作为doBegin and doCommit等方法的参数.</div>
		<div> </div>
		<div>2.输出日志:</div>
		<div>Using transaction object...</div>
		<div>如:</div>
		<div>HibernateTransactionManager 254 - Using transaction object...</div>
		<div> </div>
		<div>3.调用isExistingTransaction(...)检查事务是否存在(是否已经开始一个事务),此方法为abstract方法,需要concreate类来实现,例如hibernate</div>
		<div> </div>
		<div>4.如果isExistingTransaction为true,</div>
		<div>如果是PROPAGATION_NEVER,则抛异常</div>
		<div>PROPAGATION_NOT_SUPPORTED,则suspend当前事务并返回</div>
		<div>PROPAGATION_REQUIRES_NEW,则suspend后创建一个新事务,</div>
		<div>其它则</div>
		<div>输出日志:"Participating in existing transaction"</div>
		<div>然后</div>
		<div>处理完后返回一个TransactionStatus对象,包含是否为新transaction,是否为新的newSynchronization,suspendedResources等,<font color="#000080">getTransaction()同时也返回</font></div>
		<div>
				<font color="#000080">
				</font> </div>
		<div>
				<font color="#000080">5.</font>
				<font color="#800080">如果isExistingTransaction为false</font>
		</div>
		<div>检查超时是否小于默认时间,如果是则抛异常</div>
		<div>如果当前方法的事务属性为PROPAGATION_MANDATORY,则抛异常,否则</div>
		<div>如果当前方法的事务属性为PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED</div>
		<div>输出日志:"Creating new transaction with name ..."</div>
		<div>调用doBegin(...)创建并开始一个事务,然后返回</div>
		<div>否则返回的return newTransactionStatus(definition, null, false, newSynchronization, debugEnabled, null);<br /><div><hr /></div><div><div><font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">作者简介：</font></div><div><font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">    施祖阳，网名sylilzy,1979年生。</font></div><div><font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">    2002年起从事软件开发工作，主要研究为JAVA、Linux及相关技术。</font></div><div><font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">    你可通过</font><a href="file:///C:/DOCUME~1/SHIZY~1.GDC/LOCALS~1/Temp/WebCatcher/mailto:sylilzy@163.com"><font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">sylilzy@163.com</font></a><font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">与作者联系。</font></div></div><br /></div>
<img src ="http://www.blogjava.net/sylilzy/aggbug/52810.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sylilzy/" target="_blank">sylilzy</a> 2006-06-14 18:01 <a href="http://www.blogjava.net/sylilzy/articles/52810.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JNDI全攻略之(二)</title><link>http://www.blogjava.net/sylilzy/articles/jndiOfTwo.html</link><dc:creator>sylilzy</dc:creator><author>sylilzy</author><pubDate>Mon, 19 Dec 2005 10:11:00 GMT</pubDate><guid>http://www.blogjava.net/sylilzy/articles/jndiOfTwo.html</guid><wfw:comment>http://www.blogjava.net/sylilzy/comments/24662.html</wfw:comment><comments>http://www.blogjava.net/sylilzy/articles/jndiOfTwo.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sylilzy/comments/commentRss/24662.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sylilzy/services/trackbacks/24662.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 关键字：JNDI,J2EE,Java，命名和目录接口，Java Naming and Directory Interface<br><br>摘要：本文详细介绍了JNDI的目录相关内容，并以DNS Service Provider为例进行了示例代码的演示.本文为系列文章的第二篇，JNDI的基础内容请见本系列的第一篇<br>&nbsp;&nbsp;<a href='http://www.blogjava.net/sylilzy/articles/jndiOfTwo.html'>阅读全文</a><img src ="http://www.blogjava.net/sylilzy/aggbug/24662.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sylilzy/" target="_blank">sylilzy</a> 2005-12-19 18:11 <a href="http://www.blogjava.net/sylilzy/articles/jndiOfTwo.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java5新特征-泛型</title><link>http://www.blogjava.net/sylilzy/articles/Generic.html</link><dc:creator>sylilzy</dc:creator><author>sylilzy</author><pubDate>Mon, 19 Dec 2005 10:09:00 GMT</pubDate><guid>http://www.blogjava.net/sylilzy/articles/Generic.html</guid><wfw:comment>http://www.blogjava.net/sylilzy/comments/24661.html</wfw:comment><comments>http://www.blogjava.net/sylilzy/articles/Generic.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sylilzy/comments/commentRss/24661.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sylilzy/services/trackbacks/24661.html</trackback:ping><description><![CDATA[<DIV><FONT size=2>
<P class=diaryTitle><FONT size=3></FONT></P><FONT style="BACKGROUND-COLOR: #c0c0c0"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT style="BACKGROUND-COLOR: #ffff00">
<DIV class=diaryTitle align=center><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#000080 size=3><STRONG><FONT style="BACKGROUND-COLOR: #000000" color=#00ff00>Java5新特征-泛型</FONT><BR><BR><A href="file:///C:/DOCUME~1/shizy/LOCALS~1/Temp/WebCatcher/mailto:sylilzy@163.com"><FONT size=2>sylilzy@163.com</FONT></A></STRONG></FONT></FONT></DIV>
<DIV class=diaryTitle align=center><FONT style="BACKGROUND-COLOR: #0000ff" color=#ffffff>版权所有,转载请注明出处</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000><STRONG><BR>本文关键字</STRONG>：Generics 泛型 java泛型<BR><BR></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Generic只是提供在编译的时候提供一个类型信息，告诉编译器与某个类(如List)相关的是String型还是Double型等信息，编译时会检查调用该类中的方法时所用的类型和返回的类型是否与该类相关的类型一致,编译后的代码并无特别之处.<BR><BR></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>特别注意:J<STRONG>ava中引入泛型</STRONG></FONT></FONT><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000><STRONG>只是编译时检查，而且主要是检查调用泛型类的类，运行时并不检查,与1.4版的一样</STRONG></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000><BR>例如A类为泛型类，B类调用A类的方法，编译时将会检查B类的调用是否符合类型转换要求.对A类来说泛型类T来说具体为什么类型，是不确定的，可以看作是当Object来处理的,而且对T赋予什么对象都会出现warn,但可正常运行.<BR><BR></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>如：对于泛型类(以下称A)：<BR><BR></FONT></FONT></DIV>
<DIV style="BORDER-RIGHT: black 1px dotted; BORDER-TOP: black 1px dotted; FONT-SIZE: 10pt; BORDER-LEFT: black 1px dotted; BORDER-BOTTOM: black 1px dotted; FONT-FAMILY: Courier New"><!-- Code creation by HtmlSave Eclipse Plug-in (C) 2005 Morten Moeller / eclipse.moelleryoung.com --><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">package</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">com.sily;</SPAN></FONT><SPAN><BR><BR></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">import</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">java.util.ArrayList;</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">import</SPAN> java.util.List;<BR></FONT></SPAN><SPAN><BR></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">public</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">class</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">GenericClassA&lt;T&gt;<T></SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">{</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">public</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">T</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">p(T</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">c)</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">{</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000">T</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">t</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">(T)</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">new</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">Double(23));</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000">System.</SPAN><SPAN style="COLOR: #0000c0; FONT-STYLE: italic">out</SPAN><SPAN style="COLOR: #000000">.println(c</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #2a00ff">"&nbsp;"</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">c.getClass()+</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #2a00ff">"&nbsp;"</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">t);</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">return</SPAN><SPAN>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">c;</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #000000"><FONT style="BACKGROUND-COLOR: #ffffff">}</FONT></SPAN><SPAN><BR><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">public</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">T</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">d()</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">{</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000">T</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">t</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">(T)</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">new</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">Double(23));</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">return</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">t;</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #000000"><FONT style="BACKGROUND-COLOR: #ffffff">}</FONT></SPAN><SPAN><BR><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">public</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">List&lt;String&gt;<STRING></SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">l()</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">{</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">return</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">new</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">ArrayList&lt;String&gt;<STRING>();</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #000000"><FONT style="BACKGROUND-COLOR: #ffffff">}</FONT></SPAN><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #3f5fbf"><FONT style="BACKGROUND-COLOR: #ffffff">/**</FONT></SPAN><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #3f5fbf">*</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f9fbf">@param&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">args</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #3f5fbf"><FONT style="BACKGROUND-COLOR: #ffffff">*/</FONT></SPAN><SPAN><BR></SPAN><SPAN style="COLOR: #000000"><FONT style="BACKGROUND-COLOR: #ffffff">}</FONT></SPAN><SPAN><BR></DIV>
<DIV class=diaryTitle></FONT></FONT></FONT></FONT><FONT size=2><FONT style="BACKGROUND-COLOR: #c0c0c0"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT style="BACKGROUND-COLOR: #ffff00"><SPAN><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000></FONT></SPAN></FONT></FONT></FONT></FONT>&nbsp;</DIV>
<DIV class=diaryTitle><FONT size=2><FONT style="BACKGROUND-COLOR: #c0c0c0"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT style="BACKGROUND-COLOR: #ffff00"><SPAN><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>以下为调用类(以下称B)：</FONT></DIV></DIV>
<DIV class=diaryTitle><BR></DIV>
<DIV class=diaryTitle style="BORDER-RIGHT: black 1px dotted; BORDER-TOP: black 1px dotted; FONT-SIZE: 10pt; BORDER-LEFT: black 1px dotted; BORDER-BOTTOM: black 1px dotted; FONT-FAMILY: Courier New"><!-- Code creation by HtmlSave Eclipse Plug-in (C) 2005 Morten Moeller / eclipse.moelleryoung.com --><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">package</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">com.sily;</SPAN></FONT><SPAN><BR><BR></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">public</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">class</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">Test</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">{</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #3f5fbf"><FONT style="BACKGROUND-COLOR: #ffffff">/**</FONT></SPAN><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #3f5fbf">*</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f9fbf">@param&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">args</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #3f5fbf"><FONT style="BACKGROUND-COLOR: #ffffff">*/</FONT></SPAN><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">public</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">static</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">void</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">main(String[]</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">args)</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">{</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000">GenericClassA&lt;Double&gt;<STRING></SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">c</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">new</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">GenericClassA<STRING>&lt;Double&gt;<STRING>();</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000">String s = c.p(23125.9);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Double j = c.p(23125.34);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenericClassA&lt;String&gt; e = new GenericClassA&lt;String&gt;();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String ddd = e.d();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN></FONT><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000">GenericClassA&lt;String&gt;<DOUBLE></SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">d</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN>&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">new</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">GenericClassA&lt;String&gt;<DOUBLE>();</SPAN></FONT><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #000000"><FONT style="BACKGROUND-COLOR: #ffffff">d.p(34.23);</FONT></SPAN><SPAN><BR><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #000000"><FONT style="BACKGROUND-COLOR: #ffffff">}</FONT></SPAN><SPAN><BR></SPAN><SPAN style="COLOR: #000000"><FONT style="BACKGROUND-COLOR: #ffffff">}</FONT></SPAN><SPAN><BR></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#000080 size=3></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><FONT color=#000080 size=3><BR></FONT>会编译出错，是因为c.p(23125.9);这个方法返回的类型与定义的泛型不一致,但泛型类中的<SPAN style="COLOR: #000000">T</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">t</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">(T)</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #7f0055">new</SPAN><SPAN>&nbsp;</SPAN><SPAN style="COLOR: #000000">Double(23));只是警告.但可正常运行。</SPAN></FONT></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><SPAN style="COLOR: #000000">Double j = c.p(23125.34);这句可正常运行.</SPAN></FONT></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><SPAN style="COLOR: #000000">但是,String ddd = e.d();这句编译没问题,但运行时会出错,因为e.d()方法实际返回的是一个Double型,如果转换为String肯定出错了,如果改成:</SPAN></FONT></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><SPAN style="COLOR: #000000">Double ddd = e.d();编译时出错,因为编译器期望的返回类型是String,赋值给String型肯定编译通不过</SPAN></FONT></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><SPAN style="COLOR: #000000"></SPAN></FONT></FONT></FONT><BR><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><SPAN style="COLOR: #000000">另外，还有一点，一般都只是A类定义时用到&lt;T&gt;,而B类只是调用，所以是具体的类型，如&lt;String&gt;,&lt;Double&gt;</SPAN></FONT></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><SPAN style="COLOR: #000000">所以说，<STRONG>泛型出现的目的只是为了限制调用类B调用A时参数类型不出错的一种方式</STRONG>。</SPAN></FONT></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><SPAN style="COLOR: #000000"></SPAN></FONT></FONT></FONT><BR><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000"><FONT color=#000000 size=3><STRONG>示例分析</STRONG></FONT></SPAN></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000"></SPAN></FONT></FONT><BR><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000"><FONT color=#000000>对于调用类B中<SPAN style="COLOR: #000000">GenericClassA a=new <SPAN style="COLOR: #000000">GenericClassA ();也可看作是当Object处理，但与GenericClassA&lt;Object&gt;&nbsp;b = new GenericClassA&lt;Object&gt;();不同，如后者b.p(new Object())不会警告，但前者a.p(new Object())会警告,a的所有以泛型作参数的方法都会警告，这也是为了与1.4版本保持向下兼容</SPAN></SPAN></FONT></SPAN></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000"><SPAN style="COLOR: #000000"><FONT color=#000000><SPAN style="COLOR: #000000"></SPAN></FONT></SPAN></SPAN></FONT></FONT><BR><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000"><SPAN style="COLOR: #000000"><FONT color=#000000><SPAN style="COLOR: #000000">GenericClassA<STRING>&lt;String &gt;不能强制转换为 </SPAN>GenericClassA<STRING>&lt;Object&gt;</FONT></SPAN></SPAN></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><SPAN style="COLOR: #000000"><SPAN style="COLOR: #000000">即:</SPAN></SPAN></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff><SPAN style="COLOR: #000000"><SPAN style="COLOR: #000000">GenericClassA&lt;Object&gt; c = new GenericClassA&lt;String&gt;();</SPAN></SPAN></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff><SPAN style="COLOR: #000000"><SPAN style="COLOR: #000000">会出错</SPAN></SPAN></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><SPAN style="COLOR: #000000"></SPAN></FONT></FONT></FONT><BR><FONT style="BACKGROUND-COLOR: #0000ff"><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000>但</FONT></FONT></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff>GenericClassA a = new GenericClassA();<BR>GenericClassA&lt;String&gt; c = new GenericClassA&lt;String&gt;();<BR>a=c;</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000><FONT style="BACKGROUND-COLOR: #ffff00"><BR></FONT>不会出错,这也是为了与1.4版本保持向下兼容;</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000></FONT><BR><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff>GenericClassA&lt;?&gt; a ;<BR>GenericClassA&lt;String&gt; c = new GenericClassA&lt;String&gt;();<BR>a=c;</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000></FONT><BR><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>也不会出错</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000></FONT><BR><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>new GenericClassA&lt;?&gt;()不能实例化，因为类型不确定,调用它的相关方法如a.p("ddd")也会出错，因为类型不确定,但可调用其它方法如a.toString();另外,GenericClassA&lt;?&gt;与GenericClassA&lt;? extends Object&gt;一样</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000></FONT><BR><FONT style="BACKGROUND-COLOR: #ffffff">再看下面的代码:</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff><FONT style="BACKGROUND-COLOR: #ffff00" color=#000000><BR></FONT>GenericClassA&lt;String&gt; c = new GenericClassA&lt;String&gt;();<BR>Double dd = (Double)c.d();</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000><FONT style="BACKGROUND-COLOR: #ffff00"><BR></FONT>会出错,因为对于编译器来说,c.d();返回的就是String类型,但在GenericClassA中,</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>该方法如下:</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff>public T d() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T t = (T) (new Double(23));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t;<BR>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>所以编译时出错,因为不能编译,所以不能运行(应该是可运行的)</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>所以以下代码是正确的</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff>GenericClassA&lt;Object&gt; c = new GenericClassA&lt;Object&gt;();<BR>Double dd = (Double)c.d();</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff">实际上是对返回值作了强制类型转换</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff"></FONT><BR><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>另外,在java程序类的一个方法中,如p(Object c),则c可有"aaa"等来作参数,则会自动转换为Object ,但在泛型中&lt;Object&gt;是不能自动转换的,即GenericClassA&lt;Object&gt; c = new GenericClassA&lt;String&gt;();会出错</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>GenericClassA&lt;?&gt; c = new GenericClassA&lt;String&gt;();</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>c.p("abc");会出错</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>因为对编译器来说c是一个未知类型(可能是Object及其子类)的GenericClassA,有可能是Double的GenericClassA,所以如果p("abc")会出错</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff"></FONT><BR><FONT style="BACKGROUND-COLOR: #ffffff">补充：</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff">要成功运行以上示例代码，请下载最新版jdk：<A href="http://java.sun.com/j2se/1.5.0/download.jsp">http://java.sun.com/j2se/1.5.0/download.jsp</A></FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff">另外，Eclipse3.1已经对java5提供的支持，大家可在Eclipse3.1中调试以上代码</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff"></FONT><BR><FONT style="BACKGROUND-COLOR: #ffffff">小结:</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff">以上为对java5的新特征——泛型的一点小小的见解，更详细的内容请参考sun的tutorial:</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff"><A href="http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf">http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf</A></FONT></DIV><FONT style="BACKGROUND-COLOR: #ffffff"></FONT>
<DIV class=diaryTitle><BR><FONT style="BACKGROUND-COLOR: #ffffff">泛型只是java5的新特征之一，其它特征如变参，元数据,静态导入，增强循环等会在以后陆续介绍，敬请关注</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff"></FONT><BR><FONT style="BACKGROUND-COLOR: #ffffff">补充(2005-12-14):</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff">1.如果在spring的getHibernateTemplate().find(sql)返回的类型为List&lt;?&gt;(因为类型不确定),那么在多的UnitDAO的getUnitByName()方法中应该返回List&lt;UnitDTO&gt;,这个问题怎么解决?</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000><FONT style="BACKGROUND-COLOR: #ffff00"><BR></FONT>参考资料：</FONT></DIV>
<DIV class=diaryTitle><A href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html"><FONT style="BACKGROUND-COLOR: #ffffff">http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html</FONT></A></DIV>
<DIV>
<HR>
</DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>作者简介：</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>施祖阳，网名sylilzy,1979年生。</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>2002年起从事软件开发工作，主要研究JAVA、Linux及相关技术。</FONT></DIV>
<DIV class=diaryTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>你可通过</FONT><A href="file:///C:/DOCUME~1/shizy/LOCALS~1/Temp/WebCatcher/mailto:sylilzy@163.com"><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>sylilzy@163.com</FONT></A><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>与作者联系。</FONT></DIV></SPAN></SPAN></FONT></FONT></FONT></FONT></SPAN><img src ="http://www.blogjava.net/sylilzy/aggbug/24661.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sylilzy/" target="_blank">sylilzy</a> 2005-12-19 18:09 <a href="http://www.blogjava.net/sylilzy/articles/Generic.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JNDI全攻略之(一)</title><link>http://www.blogjava.net/sylilzy/articles/jndiOfOne.html</link><dc:creator>sylilzy</dc:creator><author>sylilzy</author><pubDate>Mon, 19 Dec 2005 10:04:00 GMT</pubDate><guid>http://www.blogjava.net/sylilzy/articles/jndiOfOne.html</guid><wfw:comment>http://www.blogjava.net/sylilzy/comments/24660.html</wfw:comment><comments>http://www.blogjava.net/sylilzy/articles/jndiOfOne.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sylilzy/comments/commentRss/24660.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sylilzy/services/trackbacks/24660.html</trackback:ping><description><![CDATA[
<META content="MSHTML 6.00.2800.1491" name=GENERATOR>
<DIV><FONT color=#000000 size=2>本文基于自己的理解，错误之处请提出宝贵意见。版权所有,转载请注明出处.</FONT></DIV>
<H3 align=center><FONT style="BACKGROUND-COLOR: #b3d9ff" color=#000000>JNDI全攻略之(一)</FONT></H3>
<DIV><FONT size=2><STRONG>关键字：JNDI,J2EE,Java，命名和目录接口，Java Naming and Directory Interface</STRONG></FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2><STRONG>名词解释</STRONG></FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;jndi是Java 命名和目录接口（Java Naming and Directory Interface，JNDI）的简称.从一开始就一直是 Java 2 平台企业版（JEE）的核心技术之一。在JMS，JMail,JDBC,EJB等技术中，就大量应用的这种技术。</FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;</FONT></DIV>
<DIV><FONT size=2><STRONG>为什么会有jndi</STRONG></FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;jndi诞生的理由似乎很简单。随着分布式应用的发展，远程访问对象访问成为常用的方法。虽然说通过Socket等编程手段仍然可实现远程通信，但按照模式的理论来说，仍是有其局限性的。RMI技术，RMI-IIOP技术的产生，使远程对象的查找成为了技术焦点。JNDI技术就应运而生。JNDI技术产生后，就可方便的查找远程或是本地对象。</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV>
<DIV><FONT size=2><STRONG>JNDI的架构与实现</STRONG></FONT></DIV>
<DIV><FONT size=2></FONT></DIV>
<DIV><FONT size=2><IMG style="WIDTH: 397px; HEIGHT: 261px" height=359 alt=r_jndiarch.jpg src="http://www.blogjava.net/images/blogjava_net/sylilzy/6184/r_jndiarch.jpg" width=575 border=0>&nbsp;&nbsp;&nbsp;&nbsp;<BR>JNDI的架构与JDBC的架构非常类似.JNDI架构提供了一组标准命名系统的API,这些API在JDK1.3之前是作为一个单独的扩展包jndi.jar(<A href="http://java.sun.com/products/jndi/downloads/index.html">通过这个地址下载</A>),这个基础API构建在与SPI之上。这个API提供如下五个包</FONT></DIV>
<UL>
<LI><A href="E:/sylilzy/book/jndi142/getStarted/overview/naming.html">javax.naming</A> 
<LI><A href="E:/sylilzy/book/jndi142/getStarted/overview/directory.html">javax.naming.directory</A> 
<LI><A href="E:/sylilzy/book/jndi142/getStarted/overview/event.html">javax.naming.event</A> 
<LI><A href="E:/sylilzy/book/jndi142/getStarted/overview/ldap.html">javax.naming.ldap</A> 
<LI><A href="E:/sylilzy/book/jndi142/getStarted/overview/provider.html">javax.naming.spi</A> </LI></UL>
<DIV><FONT size=2>在应用程序中,我们实际上只使到用以上几个包的中类.具体调用类及通信过程对用户来说是透明的.</FONT></DIV>
<DIV><FONT size=2>JNDI API提供了访问不同JNDI服务的一个标准的统一的实现,其具体实现可由不同的</FONT><A name=PROVIDER><FONT size=2>Service Provider</FONT></A><FONT size=2>来完成。前面讲的为第一层JNDI API层.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>最下层为JNDI SPI API及其具体实现。</DIV>
<DIV align=center>&nbsp;</DIV>
<DIV>图中所列的一些SPI可从</FONT><A href="http://java.sun.com/products/jndi/downloads/index.html"><FONT size=2>http://java.sun.com/products/jndi/downloads/index.html</FONT></A><FONT size=2>下载.<BR><IMG height=206 alt=r_jndispi.gif src="http://www.blogjava.net/images/blogjava_net/sylilzy/6184/r_jndispi.gif" width=230 border=0><SPAN lang=EN-US style="FONT-SIZE: 10.5pt; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">&nbsp;</v:shapetype></SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN lang=EN-US style="FONT-SIZE: 10.5pt; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><v:shapetype coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">它</v:shapetype></SPAN></FONT><FONT size=2>包括了几个增强和下面的命名/目录服务提供者：</FONT></DIV><FONT size=2>
<UL>
<LI>LDAP(Lightweight Directory Access Protocol)服务提供者 
<LI>CORBA COS（Common Object Request Broker Architecture Common Object Services）命名服务提供者&nbsp; 
<LI>RMI(Java Remote Method Invocation)注册服务提供者 
<LI>DNS(Domain Name System)服务提供者. 
<LI>FSSP(File System Service Provider)文件系统服务提供者 
<LI>其它服务提供者</LI></UL>
<DIV>&nbsp;</DIV>
<DIV>中间层为命名管理层。其功能应该由JNDI SPI来完成。上层为JNDI API,这个API包在Java 2 SDK 1.3及以上的版本中已经包括。</DIV>
<DIV>&nbsp;</DIV>
<DIV>前面讲解的只是作为应用程序客户端的架构实现,其服务端是由SPI对应的公司/厂商来实现的,我们只需将服务端的相关参数传给JNDI API就可以了,具体调用过程由SPI来完成.</FONT></DIV>
<DIV>&nbsp;</DIV></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2><STRONG>JNDI工作原理</STRONG></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=2>下面通过一个示例程序来说明JNDI工作原理(代码为自解释).</FONT></DIV>
<DIV><FONT face="Courier New" color=#008000><FONT size=2><EM>/*<BR>* Created on 2005-3-4<BR>*<BR>* To change the template for this generated file go to<BR>* Window&amp;gt;Preferences&amp;gt;Java&amp;gt;Code Generation&amp;gt;Code and Comments<BR>*/<BR></EM><FONT color=#ff0000><B>package </B></FONT><FONT color=#000000>com</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>sily</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>jndi</FONT><B><FONT color=#0000ff><FONT size=2>;<BR><BR></FONT></B></FONT><FONT size=2><FONT color=#ff0000><B>import </B></FONT><FONT color=#000000>java</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>io</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>FileInputStream</FONT><B><FONT color=#0000ff><FONT size=2>;<BR></FONT></B></FONT><FONT size=2><FONT color=#ff0000><B>import </B></FONT><FONT color=#000000>java</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>util</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>Properties</FONT><B><FONT color=#0000ff><FONT size=2>;<BR><BR></FONT></B></FONT><FONT size=2><FONT color=#ff0000><B>import </B></FONT><FONT color=#000000>javax</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>naming</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>Context</FONT><B><FONT color=#0000ff><FONT size=2>;<BR></FONT></B></FONT><FONT size=2><FONT color=#ff0000><B>import </B></FONT><FONT color=#000000>javax</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>naming</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>InitialContext</FONT><B><FONT color=#0000ff><FONT size=2>;<BR><BR></FONT></B></FONT><FONT size=2><FONT color=#3a6ea5>/**<BR>* @author shizy<BR>*<BR>* To change the template for this generated type comment go to<BR>* Window&amp;gt;Preferences&amp;gt;Java&amp;gt;Code Generation&amp;gt;Code and Comments<BR>*/<BR></FONT><FONT color=#ff0000><B>public class </B></FONT><FONT color=#000000>TestJbossJNDI </FONT></FONT><B><FONT color=#0000ff><FONT size=2>{<BR></FONT></B></FONT><FONT color=#000000></FONT><FONT size=2><FONT color=#3a6ea5>/**<BR>* <BR>*/<BR></FONT><FONT color=#000000></FONT><FONT color=#ff0000><B>public </B></FONT><FONT color=#000000>TestJbossJNDI</FONT></FONT><B><FONT color=#0000ff><FONT size=2>() {<BR></FONT></B></FONT><FONT color=#000000></FONT><FONT color=#ff0000 size=2><B>super</B></FONT><FONT color=#0000ff size=2><B>();<BR></B></FONT><FONT color=#000000></FONT><FONT color=#008000 size=2><EM>// TODO Auto-generated constructor stub<BR></EM></FONT><FONT color=#000000></FONT><FONT size=2><FONT color=#0000ff><B>}<BR></B></FONT><FONT color=#000000></FONT><FONT color=#ff0000><B>public static void </B></FONT><FONT color=#000000>main</FONT><B><FONT color=#0000ff>(</B></FONT></FONT><FONT size=2><FONT color=#000000>String</FONT><B><FONT color=#0000ff>[] </B></FONT></FONT><FONT size=2><FONT color=#000000>args</FONT><B><FONT color=#0000ff>) {</B></FONT></FONT><FONT size=2><FONT color=#000000> </FONT><FONT color=#ff0000><B>try </B></FONT></FONT><FONT size=2><FONT color=#0000ff><B>{<BR></B></FONT><FONT color=#000000>Properties env </FONT><B><FONT color=#0000ff>= </B></FONT></FONT><FONT size=2><FONT color=#ff0000><B>new </B></FONT><FONT color=#000000>Properties</FONT><FONT color=#0000ff><B>();</B></FONT></FONT></FONT></DIV>
<DIV><FONT face="Courier New" color=#008000 size=3><FONT size=2><FONT color=#0000ff><FONT color=#008000>//载入jboss的SPI相关参数,包括初始上下文工厂，服务URL，等等</FONT><BR></FONT><FONT color=#000000>env</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>load</FONT><B><FONT color=#0000ff>(</B></FONT></FONT><FONT size=2><FONT color=#ff0000><B>new </B></FONT><FONT color=#000000>FileInputStream</FONT></FONT><FONT color=#0000ff><FONT size=2><B>(</B>"jbossJndi.properties"</FONT><B><FONT size=2>));<BR></FONT></B></FONT><FONT size=2><FONT color=#000000>env</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>list</FONT><B><FONT color=#0000ff>(</B></FONT></FONT><FONT size=2><FONT color=#000000>System</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>out</FONT><FONT size=2><FONT color=#0000ff><B>);</B></FONT></FONT></FONT></DIV>
<DIV><FONT face="Courier New" color=#008000 size=3><FONT size=2><FONT color=#0000ff><FONT color=#008000>//通过JNDI api 初始化上下文</FONT><BR></FONT><FONT color=#000000>InitialContext ctx </FONT><B><FONT color=#0000ff>= </B></FONT></FONT><FONT size=2><FONT color=#ff0000><B>new </B></FONT><FONT color=#000000>javax</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>naming</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>InitialContext</FONT><B><FONT color=#0000ff>(</B></FONT></FONT><FONT color=#000000 size=2>env</FONT><FONT size=2><FONT color=#0000ff><B>);<BR></B></FONT><FONT color=#000000>System</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>out</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>println</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"Got context"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT color=#000000></FONT><FONT size=2><FONT color=#008000><EM>//create a subContext<BR></EM></FONT><FONT color=#000000>ctx</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>createSubcontext</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"/sylilzy"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT size=2><FONT color=#000000>ctx</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>createSubcontext</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"sylilzy/sily"</FONT><B><FONT size=2>);</FONT></B></FONT></FONT></DIV>
<DIV><FONT face="Courier New" color=#008000 size=3><FONT color=#0000ff><FONT size=2><FONT color=#008000>//rebind a object</FONT><BR></FONT></FONT><FONT size=2><FONT color=#000000>ctx</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>rebind</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"sylilzy/sily/a"<B>, </B>"I am sily a!"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT size=2><FONT color=#000000>ctx</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>rebind</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"sylilzy/sily/b"<B>, </B>"I am sily b!"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT color=#000000><BR></FONT><FONT size=2><FONT color=#008000><EM>//lookup context<BR></EM></FONT><FONT color=#000000>Context ctx1</FONT><B><FONT color=#0000ff>=(</B></FONT></FONT><FONT size=2><FONT color=#000000>Context</FONT><B><FONT color=#0000ff>)</B></FONT></FONT><FONT size=2><FONT color=#000000>ctx</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>lookup</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"sylilzy"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT size=2><FONT color=#000000>Context ctx2</FONT><B><FONT color=#0000ff>=(</B></FONT></FONT><FONT size=2><FONT color=#000000>Context</FONT><B><FONT color=#0000ff>)</B></FONT></FONT><FONT size=2><FONT color=#000000>ctx1</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>lookup</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"/sylilzy/sily"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT size=2><FONT color=#000000>ctx2</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>bind</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"/sylilzy/g"<B>, </B>"this is g"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT color=#000000></FONT><FONT size=2><FONT color=#008000><EM>//lookup binded object<BR></EM></FONT><FONT color=#000000>Object o</FONT></FONT><FONT size=2><FONT color=#0000ff><B>;<BR></B></FONT><FONT color=#000000>o</FONT><B><FONT color=#0000ff>=</B></FONT></FONT><FONT size=2><FONT color=#000000>ctx1</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>lookup</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"sily/a"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT size=2><FONT color=#000000>System</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>out</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>println</FONT><B><FONT color=#0000ff>(</B>"get object from jndi:"<B>+</B></FONT></FONT><FONT color=#000000 size=2>o</FONT><FONT color=#0000ff size=2><B>);<BR></B></FONT><FONT color=#000000></FONT><FONT size=2><FONT color=#008000><EM>//rename the object<BR></EM></FONT><FONT color=#000000>ctx2</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>rename</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"/sylilzy/g"<B>, </B>"g1"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT size=2><FONT color=#000000>o</FONT><B><FONT color=#0000ff>=</B></FONT></FONT><FONT size=2><FONT color=#000000>ctx2</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>lookup</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"g1"</FONT><B><FONT size=2>);<BR></FONT></B></FONT><FONT size=2><FONT color=#000000>System</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>out</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT size=2><FONT color=#000000>println</FONT><B><FONT color=#0000ff>(</B>"get object from jndi:"<B>+</B></FONT></FONT><FONT color=#000000 size=2>o</FONT><FONT size=2><FONT color=#0000ff><B>);<BR><BR></B></FONT><FONT color=#000000></FONT><FONT color=#0000ff><B>} </B></FONT><FONT color=#ff0000><B>catch </B></FONT><FONT color=#0000ff><B>(</B></FONT><FONT color=#000000>Exception e</FONT></FONT><FONT size=2><FONT color=#0000ff><B>) {<BR></B></FONT><FONT color=#000000>e</FONT><B><FONT color=#0000ff>.</B></FONT></FONT><FONT color=#000000 size=2>printStackTrace</FONT><FONT color=#0000ff size=2><B>();<BR></B></FONT><FONT color=#000000></FONT><FONT color=#0000ff size=2><B>}<BR></B></FONT><FONT color=#000000></FONT><FONT color=#0000ff><B><FONT size=2>}<BR>}<BR></FONT></DIV></B></FONT></FONT>
<DIV><FONT size=2>结果输出如下:</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>-- listing properties --<BR>java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory<BR>java.naming.provider.url=jnp://localhost:1099<BR>java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces<BR>Got context<BR>get object from jndi:I am sily a!<BR>get object from jndi:this is g</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>程序中<FONT face="Courier New" color=#0000ff>jbossJndi.properties文件的内容为:</FONT></FONT></DIV>
<DIV><FONT size=2>java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory<BR>java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces<BR>java.naming.provider.url=jnp://localhost:1099</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>注意:要正确运行示例程序,请启动jboss,并将jboss的jbossall-client.jar文件放入classpath中。</FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>上述示例程序在jboss服务器的jndi树上建立了几个上下文,并bind了几对象,大家可通过附录中的代码或其它工具查看</FONT></DIV>
<DIV><FONT size=2>查看结果为：</FONT></DIV>
<DIV><FONT size=2>-----------------------------<BR>/sylilzy/sily<BR>-----------------------------<BR>/sylilzy/sily/b:I am sily b!<BR>/sylilzy/sily/a:I am sily a!<BR>/sylilzy/sily/g1:this is g<BR>-----------------------------<BR>-----------------------------</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>上述程序中，我们的代码只涉及到了jndi API，其它细节如初始化jboss jndi的初始上下文，建立网络连接，与服务器通信，对我们来说都是透明的，另外，我们将jboss jndi的spi包中的类名作为参数传入了程序中,要访问一个远程对象，我们所做的就这么多。</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>下面，再提供一个例子，与上例不同，我们不需要jboss,我们使用sun的FSSP(File System Service Provider)文件系统服务提供者.注意在这个例子中要使用到前面所说的File System Service Provider for the Java Naming and Directory Interface<SUP>TM</SUP> (JNDI)相关类（<A href="http://java.sun.com/products/jndi/downloads/index.html">下载</A>）。</FONT></DIV><PRE><CODE><FONT face="Courier New" size=3><FONT size=2><FONT color=#008000><I>/*
 * Created on 2005-3-1
 *
 * To change the template for this generated file go to
 * Window&amp;gt;Preferences&amp;gt;Java&amp;gt;Code Generation&amp;gt;Code and Comments
 */
</I></FONT><FONT color=#ff0000><B>package </B></FONT>com<FONT color=#0000ff><B>.</B></FONT>sily<FONT color=#0000ff><B>.</B></FONT>jndi</FONT><FONT size=2><FONT color=#0000ff><B>;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>java<FONT color=#0000ff><B>.</B></FONT>io<FONT color=#0000ff><B>.</B></FONT>FileInputStream</FONT><FONT size=2><FONT color=#0000ff><B>;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>java<FONT color=#0000ff><B>.</B></FONT>util<FONT color=#0000ff><B>.</B></FONT>Properties</FONT><FONT size=2><FONT color=#0000ff><B>;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>javax<FONT color=#0000ff><B>.</B></FONT>naming</FONT><FONT size=2><FONT color=#0000ff><B>.*;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>javax<FONT color=#0000ff><B>.</B></FONT>naming<FONT color=#0000ff><B>.</B></FONT>Context</FONT><FONT size=2><FONT color=#0000ff><B>;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>javax<FONT color=#0000ff><B>.</B></FONT>naming<FONT color=#0000ff><B>.</B></FONT>InitialContext</FONT><FONT color=#0000ff size=2><B>;
</B></FONT><FONT size=2><FONT color=#3a6ea5>/**
 * @author shizy
 *
 * To change the template for this generated type comment go to
 * Window&amp;gt;Preferences&amp;gt;Java&amp;gt;Code Generation&amp;gt;Code and Comments
 */
</FONT><FONT color=#ff0000><B>public class </B></FONT>JndiTest1 </FONT><FONT size=2><FONT color=#0000ff><B>{
</B></FONT><FONT color=#000000>	</FONT></FONT><FONT size=2><FONT color=#3a6ea5>/**
	 * 
	 */
</FONT><FONT color=#000000>	</FONT><FONT color=#ff0000><B>public </B></FONT>JndiTest1</FONT><FONT size=2><FONT color=#0000ff><B>() {
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>super</B></FONT></FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>		</FONT></FONT><FONT size=2><FONT color=#008000><I>// TODO Auto-generated constructor stub
</I></FONT><FONT color=#000000>	</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>	</FONT><FONT color=#ff0000><B>public static void </B></FONT>main<FONT color=#0000ff><B>(</B></FONT>String<FONT color=#0000ff><B>[] </B></FONT>args</FONT><FONT size=2><FONT color=#0000ff><B>) {
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>try </B></FONT></FONT><FONT size=2><FONT color=#0000ff><B>{
</B></FONT><FONT color=#000000>			</FONT>Properties env <FONT color=#0000ff><B>= </B></FONT><FONT color=#ff0000><B>new </B></FONT>Properties</FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>			</FONT>env<FONT color=#0000ff><B>.</B></FONT>load<FONT color=#0000ff><B>(</B></FONT><FONT color=#ff0000><B>new </B></FONT>FileInputStream</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"fileSystemService.properties"</FONT><B><FONT size=2>));
</FONT></B></FONT><FONT size=2><FONT color=#000000>			</FONT>env<FONT color=#0000ff><B>.</B></FONT>put<FONT color=#0000ff><B>(</B></FONT>Context<FONT color=#0000ff><B>.</B></FONT>PROVIDER_URL</FONT><FONT color=#0000ff><FONT size=2><B>, </B>"file:///c:/"</FONT><B><FONT size=2>);
</FONT></B></FONT><FONT size=2><FONT color=#000000>			</FONT>Context ctx <FONT color=#0000ff><B>= </B></FONT><FONT color=#ff0000><B>new </B></FONT>InitialContext<FONT color=#0000ff><B>(</B></FONT>env</FONT><FONT size=2><FONT color=#0000ff><B>);
</B></FONT><FONT color=#000000>			</FONT>ctx<FONT color=#0000ff><B>.</B></FONT>createSubcontext</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"sylilzy"</FONT><B><FONT size=2>);
</FONT></B></FONT><FONT size=2><FONT color=#000000>			
			</FONT>NamingEnumeration list <FONT color=#0000ff><B>= </B></FONT>ctx<FONT color=#0000ff><B>.</B></FONT>list</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"/"</FONT><B><FONT size=2>);
</FONT></B></FONT><FONT size=2><FONT color=#000000>			</FONT><FONT color=#ff0000><B>while </B></FONT><FONT color=#0000ff><B>(</B></FONT>list<FONT color=#0000ff><B>.</B></FONT>hasMore</FONT><FONT size=2><FONT color=#0000ff><B>()) {
</B></FONT><FONT color=#000000>				</FONT>NameClassPair nc <FONT color=#0000ff><B>= (</B></FONT>NameClassPair<FONT color=#0000ff><B>) </B></FONT>list<FONT color=#0000ff><B>.</B></FONT>next</FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>				</FONT>System<FONT color=#0000ff><B>.</B></FONT>out<FONT color=#0000ff><B>.</B></FONT>println<FONT color=#0000ff><B>(</B></FONT>nc</FONT><FONT size=2><FONT color=#0000ff><B>);
</B></FONT><FONT color=#000000>			</FONT></FONT><FONT color=#0000ff size=2><B>}
</B></FONT><FONT color=#000000 size=2>			
		</FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>catch </B></FONT><FONT color=#0000ff><B>(</B></FONT>Exception e</FONT><FONT size=2><FONT color=#0000ff><B>) {
</B></FONT><FONT color=#000000>			</FONT>e<FONT color=#0000ff><B>.</B></FONT>printStackTrace</FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>		</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>	</FONT></FONT><FONT color=#0000ff><B><FONT size=2>}
}</FONT></B></FONT></FONT>
</CODE><STRONG><FONT size=2></FONT></STRONG></PRE>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>上例中fileSystemService.properties文件的内容为：java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>这个例子较简单，运行后，它会列出C:\下所有的文件和目录，另外你会发现有一个新目录被创建了.本例不同于上例，它并不需要服务端，因为它访问的是文件系统.有关帮助可查阅包内的相关文档。</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>通过对比这两个例子，应该JNDI的工作原理有了一个大致的了解。</FONT></DIV><PRE><STRONG><FONT size=2>总结：</FONT></STRONG></PRE>
<DIV><FONT size=2>jndi技术体现了分布式应用的优点，同进它的产生也为分布式对象提供了统一的访问接口。由于篇幅所限，对目录的操作本文未作介绍，其它内容将在接下来的系列中讨论。要对JNDI技术作全面的了解，请参阅参考资料.要对于JNDI技术深入学习，仍有许多地方值得进一步了解，例如EJB容器所使用的JNDI所提供的对象就有 Local和Remote之分，对于Local Object,对于不同的JVM是不可访问的；对于远程对象的访问，还涉及到Java安全机制。</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><STRONG><FONT size=2>附录:</FONT></STRONG></DIV>
<DIV><FONT size=2>查看jboss jndi内容的代码:</FONT></DIV>
<DIV><FONT size=2>//----------------------------------------</FONT></DIV>
<DIV><PRE><CODE><FONT face="Courier New"><FONT size=2><FONT color=#008000><I>/*
 * Created on 2005-3-4
 *
 * To change the template for this generated file go to
 * Window&amp;gt;Preferences&amp;gt;Java&amp;gt;Code Generation&amp;gt;Code and Comments
 */
</I></FONT><FONT color=#ff0000><B>package </B></FONT>com<FONT color=#0000ff><B>.</B></FONT>sily<FONT color=#0000ff><B>.</B></FONT>jndi</FONT><FONT size=2><FONT color=#0000ff><B>;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>java<FONT color=#0000ff><B>.</B></FONT>io<FONT color=#0000ff><B>.</B></FONT>FileInputStream</FONT><FONT size=2><FONT color=#0000ff><B>;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>java<FONT color=#0000ff><B>.</B></FONT>util<FONT color=#0000ff><B>.</B></FONT>Properties</FONT><FONT size=2><FONT color=#0000ff><B>;

</B></FONT><FONT color=#ff0000><B>import </B></FONT>javax<FONT color=#0000ff><B>.</B></FONT>naming</FONT><FONT size=2><FONT color=#0000ff><B>.*;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>javax<FONT color=#0000ff><B>.</B></FONT>naming<FONT color=#0000ff><B>.</B></FONT>Context</FONT><FONT size=2><FONT color=#0000ff><B>;
</B></FONT><FONT color=#ff0000><B>import </B></FONT>javax<FONT color=#0000ff><B>.</B></FONT>naming<FONT color=#0000ff><B>.</B></FONT>InitialContext</FONT><FONT color=#0000ff size=2><B>;
</B></FONT><FONT size=2><FONT color=#3a6ea5>/**
 * @author shizy
 *
 * To change the template for this generated type comment go to
 * Window&amp;gt;Preferences&amp;gt;Java&amp;gt;Code Generation&amp;gt;Code and Comments
 */
</FONT><FONT color=#ff0000><B>public class </B></FONT>ListJbossJndi </FONT><FONT size=2><FONT color=#0000ff><B>{
</B></FONT><FONT color=#000000>	</FONT></FONT><FONT size=2><FONT color=#3a6ea5>/**
	 * 
	 */
</FONT><FONT color=#000000>	</FONT><FONT color=#ff0000><B>public </B></FONT>ListJbossJndi</FONT><FONT size=2><FONT color=#0000ff><B>() {
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>super</B></FONT></FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>		</FONT></FONT><FONT size=2><FONT color=#008000><I>// TODO Auto-generated constructor stub
</I></FONT><FONT color=#000000>	</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>	</FONT><FONT color=#ff0000><B>public static void </B></FONT>main<FONT color=#0000ff><B>(</B></FONT>String<FONT color=#0000ff><B>[] </B></FONT>args</FONT><FONT size=2><FONT color=#0000ff><B>) {
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>try </B></FONT></FONT><FONT size=2><FONT color=#0000ff><B>{
</B></FONT><FONT color=#000000>			</FONT>Properties env <FONT color=#0000ff><B>= </B></FONT><FONT color=#ff0000><B>new </B></FONT>Properties</FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>			</FONT>env<FONT color=#0000ff><B>.</B></FONT>load<FONT color=#0000ff><B>(</B></FONT><FONT color=#ff0000><B>new </B></FONT>FileInputStream</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"jbossJndi.properties"</FONT><B><FONT size=2>));
</FONT></B></FONT><FONT color=#000000 size=2>			</FONT><FONT size=2><FONT color=#008000><I>//env.list(System.out);
</I></FONT><FONT color=#000000>			</FONT>Context ctx <FONT color=#0000ff><B>= </B></FONT><FONT color=#ff0000><B>new </B></FONT>InitialContext<FONT color=#0000ff><B>(</B></FONT>env</FONT><FONT size=2><FONT color=#0000ff><B>);
</B></FONT><FONT color=#000000>			</FONT>listCtx<FONT color=#0000ff><B>(</B></FONT>ctx<FONT color=#0000ff><B>.</B></FONT>lookup</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"sylilzy"</FONT><B><FONT size=2>));
</FONT></B></FONT><FONT color=#000000 size=2>		</FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>catch </B></FONT><FONT color=#0000ff><B>(</B></FONT>Exception e</FONT><FONT size=2><FONT color=#0000ff><B>) {
</B></FONT><FONT color=#000000>			</FONT>e<FONT color=#0000ff><B>.</B></FONT>printStackTrace</FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>		</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>	</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>	</FONT><FONT color=#ff0000><B>static void </B></FONT>listCtx<FONT color=#0000ff><B>(</B></FONT>Object o</FONT><FONT size=2><FONT color=#0000ff><B>){
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>if</B></FONT><FONT color=#0000ff><B>(!(</B></FONT>o <FONT color=#ff0000><B>instanceof </B></FONT>Context<FONT color=#0000ff><B>))</B></FONT>log<FONT color=#0000ff><B>(</B>":"<B>+</B></FONT>o</FONT><FONT size=2><FONT color=#0000ff><B>);
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>else </B></FONT></FONT><FONT size=2><FONT color=#0000ff><B>{
</B></FONT><FONT color=#000000>			</FONT>log</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"\n-----------------------------"</FONT><B><FONT size=2>);
</FONT></B></FONT><FONT size=2><FONT color=#000000>		</FONT><FONT color=#ff0000><B>try </B></FONT></FONT><FONT size=2><FONT color=#0000ff><B>{
</B></FONT><FONT color=#000000>			</FONT>Context ctx<FONT color=#0000ff><B>=(</B></FONT>Context<FONT color=#0000ff><B>)</B></FONT>o</FONT><FONT size=2><FONT color=#0000ff><B>;
</B></FONT><FONT color=#000000>			</FONT></FONT><FONT size=2><FONT color=#008000><I>//log(ctx.getNameInNamespace()+"/:");
</I></FONT><FONT color=#000000>			</FONT>NamingEnumeration list<FONT color=#0000ff><B>=</B></FONT>ctx<FONT color=#0000ff><B>.</B></FONT>listBindings</FONT><FONT color=#0000ff><FONT size=2><B>(</B>""</FONT><B><FONT size=2>);
</FONT></B></FONT><FONT size=2><FONT color=#000000>			</FONT><FONT color=#ff0000><B>while</B></FONT><FONT color=#0000ff><B>(</B></FONT>list<FONT color=#0000ff><B>.</B></FONT>hasMore</FONT><FONT size=2><FONT color=#0000ff><B>()){
</B></FONT><FONT color=#000000>				</FONT>Binding bind<FONT color=#0000ff><B>=(</B></FONT>Binding<FONT color=#0000ff><B>)</B></FONT>list<FONT color=#0000ff><B>.</B></FONT>next</FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>				</FONT>log<FONT color=#0000ff><B>(</B>"\n/"<B>+</B></FONT>ctx<FONT color=#0000ff><B>.</B></FONT>getNameInNamespace<FONT color=#0000ff><B>()+</B>"/"<B>+</B></FONT>bind<FONT color=#0000ff><B>.</B></FONT>getName</FONT><FONT size=2><FONT color=#0000ff><B>());
</B></FONT><FONT color=#000000>				</FONT>listCtx<FONT color=#0000ff><B>(</B></FONT>bind<FONT color=#0000ff><B>.</B></FONT>getObject</FONT><FONT size=2><FONT color=#0000ff><B>());
</B></FONT><FONT color=#000000>			</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>			</FONT>log</FONT><FONT color=#0000ff><FONT size=2><B>(</B>"\n-----------------------------"</FONT><B><FONT size=2>);
</FONT></B></FONT><FONT color=#000000 size=2>		</FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>		</FONT><FONT color=#ff0000><B>catch </B></FONT><FONT color=#0000ff><B>(</B></FONT>NamingException e</FONT><FONT size=2><FONT color=#0000ff><B>) {
</B></FONT><FONT color=#000000>			</FONT></FONT><FONT size=2><FONT color=#008000><I>// TODO Auto-generated catch block
</I></FONT><FONT color=#000000>			</FONT>e<FONT color=#0000ff><B>.</B></FONT>printStackTrace</FONT><FONT size=2><FONT color=#0000ff><B>();
</B></FONT><FONT color=#000000>		</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>	</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>	</FONT></FONT><FONT size=2><FONT color=#0000ff><B>}
</B></FONT><FONT color=#000000>	</FONT><FONT color=#ff0000><B>static void </B></FONT>log<FONT color=#0000ff><B>(</B></FONT>Object o</FONT><FONT size=2><FONT color=#0000ff><B>){
</B></FONT><FONT color=#000000>		</FONT>System<FONT color=#0000ff><B>.</B></FONT>out<FONT color=#0000ff><B>.</B></FONT>print<FONT color=#0000ff><B>(</B></FONT>o</FONT><FONT size=2><FONT color=#0000ff><B>);
</B></FONT><FONT color=#000000>	</FONT></FONT><FONT color=#0000ff size=2><B>}
}
</B></FONT></FONT>
</CODE></PRE><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>
<HR>
</FONT></DIV>
<DIV><FONT size=2>作者简介：</FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;施祖阳，网名sylilzy,1979年生。</FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;2002年起从事软件开发工作，主要研究为JAVA、Linux及相关技术。</FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;你可通过</FONT><A href="C:/DOCUME~1/SHIZY~1.GDC/LOCALS~1/Temp/WebCatcher/mailto:sylilzy@163.com"><FONT size=2>sylilzy@163.com</FONT></A><FONT size=2>与作者联系。</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>参考资料：</FONT></DIV>
<DIV><FONT size=2>1.<FONT size=3><A href="http://java.sun.com/products/jndi/tutorial/">http://java.sun.com/products/jndi/tutorial/</A></FONT></FONT><FONT size=2></FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV><img src ="http://www.blogjava.net/sylilzy/aggbug/24660.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sylilzy/" target="_blank">sylilzy</a> 2005-12-19 18:04 <a href="http://www.blogjava.net/sylilzy/articles/jndiOfOne.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>