??xml version="1.0" encoding="utf-8" standalone="yes"?>一本一本久久a久久综合精品,91精品电影,久久精品麻豆http://www.aygfsteel.com/sylilzy/category/5917.html我学故我知,我思故我在Qjava你我Qhappy你我——sylilzyzh-cnThu, 01 Mar 2007 02:33:08 GMTThu, 01 Mar 2007 02:33:08 GMT60Java与数字签?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/sylilzy/articles/JavaAdnDigtalSign.html</guid><wfw:comment>http://www.aygfsteel.com/sylilzy/comments/56929.html</wfw:comment><comments>http://www.aygfsteel.com/sylilzy/articles/JavaAdnDigtalSign.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/sylilzy/comments/commentRss/56929.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/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">版权所?转蝲h明出?/font> </p> <p>关键字:Java 数字{֐ PKI Keystore 数字证书 keytool jarsigner</p> <p>摘要Q本文介l了数字{֐的相兛_知识Qƈ介绍了如何用java实现数字{֐?/p> <p class="table-list"> <strong class="headWord">?/strong>字签名作ZU电子n份的认证的手D?被普遍用于网上银?安全|络通信{领?数字{֐是电子签名的一U特定Ş?本文不对数字{֐的原理作介绍,只对相关概念作一些简单的介绍,详细讲解了在java中如何对jar文gq行数字{֐.</p> <p class="highLight2">数字{֐的相x?/p> <p>实际上数字签名又UCZPKI的电子签? PKI的核心机构是电子认证服务提供者,即通称的认证机构CAQPKI{֐的核心元素是由CA{֏的数字证?数字证书如同日常生zM的n份证一?用来标识|上实体的n?CA是对网上实体进行认证的W三Ҏ?数字证书是CA机构对网上实体进行认证而生的电子证书,它是数据{֐的基技术保?CA机构对电子证书的有效?有效期等提供查询服务.数字证书所包含公钥用来对用对应的U钥加密的数据信息进行验?<br /><br />数字{֐实现的具体原理是Q?<br />1?报文按双方U定的HASH法计算得到一个固定位数的报文摘要。在数学上保证,只要改动报文中Q何一位,重新计算出的报文摘要值就会与原先的g相符。这样就保证了报文的不可更改性?详见参考资料的"公钥密码技术原?章节)<br />2?该报文摘要值用发送者的Uh密钥加密Q然后连同原报文和数字证书(包含公钥Q一起发送给接收者而生的报文即称数字{֐?br />3、接收方收到数字{֐后,用同LHASH法Ҏ文计摘要|然后与用发送者的公开密钥q行解密解开的报文摘要值相比较Q如相等则说明报文确实来自所U的发送者?<br />4、同旉过证书颁发机构CA认证书的有效性即可确认发送的真实w䆾?br /><br />数字证书的技术实?<br />数字证书是公钥的载体,是PKI的核心元?数字证书W合X.509标准,现行的PIK机制一般ؓ又证?即一个实体一般有两个证书,两个密码?一个用于电子签?一个用于加密通信.按照X.509标准,一个标准和数字证书的格式ؓ:<br /><br />CA《A?CA{VQSNQAIQCAQUCAQAQUAQApQTa}</p> <p>它将包含证书颁发机构和标识及用户的标?证书ID,有效期等信息(详见参考资?,另外q包含CAҎ证书内容的进行了数字{֐,以验证此证书的有效?在验证一个数字证书的q程?Ҏ字证书的数字{֐的验证将递归q行,只到扑ֈ双方共同可信ȝCA根证书ؓ?</p> <p class="highLight2">如何获取数字证书</p> <p>数字证书可向专门的CA机构甌,有免费的数字证书和付费的数字证书.比如:中国数字认证|?http://www.ca365.com)、广东省电子商务认证中心(http://www.cnca.net/)可甌到有效期Zq的免费数字证书.注意甌的证书是以该CA的证书作为申L个h证书的根证书,所以如果想要申L证书有效,需要下载ƈ安装相应CA的根证书.另外,其它一些工具也可生成个Z用的数字证书,如微软的makecert(下蝲地址http://www.microsoft.com/downloads/details.aspx?familyid=2b742795-d0f0-4a66-b27f-22a95fcd3425&displaylang=en))QJDK中包含的keytool工具(以下会详l介l如何?都可生成没有l过认证的数字证? </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文gq行{֐使用的工hjarsigner:</p> <p>格式如下:<span id="wmqeeuq" class="highLight4">jarsigner -keystore keystoreFile jarfile alias</span></p> <p>例如:<span id="wmqeeuq" class="highLight4">jarsigner -keystore sylilzyKeystore.dat test.jar sylilzy</span></p> <p>验证一个jar文g如下:</p> <p>jarsigner -verify jarfile</p> <p>以下步骤演示了一个完整的p取数字证书到{֐jar文g,然后讉K该jar文g时显C签名信息的全过E?</p> <p>上面提到,数字证书可由以下方式生成,W一,去http://www.ca365.com甌一个免费的数字证书,下蝲后ؓtest.der</p> <p>也可用keytool -genkey -alias mykey -keystore mystore生成,q行此命令后,提示相关信息,q填写完?如下:</p> <p class="table-info">E:\temp>keytool -genkey -alias mykey -keystore mystore<br />输入keystore密码Q?111111<br />您的名字与姓氏是什么?<br />[Unknown]Q?shi<br />您的l织单位名称是什么?<br />[Unknown]Q?eshore<br />您的l织名称是什么?<br />[Unknown]Q?de<br />您所在的城市或区域名U是什么?<br />[Unknown]Q?gz<br />您所在的州或省䆾名称是什么?<br />[Unknown]Q?gd<br />该单位的两字母国家代码是什?br />[Unknown]Q?cn<br />CN=shi, OU=eshore, O=de, L=gz, ST=gd, C=cn 正确吗?<br />[否]Q?y<br />输入<mykey>的主密码<br />Q如果和 keystore 密码相同Q按回RQ:</p> <p>到此,我们则生成了一个名为mystore的keystore文g,该文件中保存有别名ؓmykey的数字证?输入以下命o卛_查看此数字证?</p> <p class="table-info">E:\temp>keytool -list -alias mykey -keystore mystore<br />输入keystore密码Q?111111<br />mykey, 2006-7-5, keyEntry,<br />认证指纹 (MD5)Q?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>keytool -list -alias mykey -keystore mystore -v<br />输入keystore密码Q?111111<br />别名名称Q?mykey<br />创徏日期Q?2006-7-5<br />输入cdQKeyEntry<br />认证N度: 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 />序号Q?44ab3fb8<br />有效期间Q?Wed Jul 05 12:27:36 CST 2006 臻I Tue Oct 03 12:27:36 CST 2006<br />认证指纹Q?br />MD5Q?A1:A9:A7:C6:D8:E5:E5:20:EA:80:59:AF:C2:65:B4:17<br />SHA1Q?AC:8D:AA:9D:A7:62:48:70:ED:F4:28:4C:DC:DE:56:CE:41:FE:52:C9</p> <p>以上为创建的一个证?我们可以它从keystore文g中导?</p> <p class="table-info">E:\temp>keytool -export -keystore mystore -alias mykey -file my.cer<br />输入keystore密码Q?111111<br />保存在文件中的认?<my.cer></p> <p>我们也可前面从|上甌的电子证书导入keystore文g:</p> <p class="table-info">E:\temp>keytool -import -alias sily -file test.der -keystore mystore<br />输入keystore密码Q?111111<br />Owner: CN=施祖? OU=develop, O=eshore, L=q州, ST=q东, C=CN<br />发照者: CN=CA365 Free Root Certificate, O=CA365, L=Beijing, ST=Beijing, C=CN<br />序号Q?653e1a63bb003fb3<br />有效期间Q?Tue Jul 04 12:02:25 CST 2006 臻I Wed Jul 04 12:02:25 CST 2007<br />认证指纹Q?br />MD5Q?4A:AA:63:2A:8A:E2:8D:76:4B:2B:73:E9:F3:03:CD:6F<br />SHA1Q?02:B3:9E:D0:7E:BB:9E:C4:B3:B0:79:19:FA:89:B6:93:DB:0F:4A:88<br />信Qq个认证Q?[否]Q?y<br />认证已添加至keystore?/p> <p>现在查看mystore中已l存了两个证?</p> <p class="table-info">E:\temp>keytool -list -keystore mystore<br />输入keystore密码Q?111111<br />Keystore cdQ?jks<br />Keystore 提供者: SUN<br />您的 keystore 包含 2 输入<br />sily, 2006-7-5, trustedCertEntry,<br />认证指纹 (MD5)Q?4A:AA:63:2A:8A:E2:8D:76:4B:2B:73:E9:F3:03:CD:6F<br />mykey, 2006-7-5, keyEntry,<br />认证指纹 (MD5)Q?A1:A9:A7:C6:D8:E5:E5:20:EA:80:59:AF:C2:65:B4:17</p> <p>现在,我们开始利用前面生成的数字证书ljar文g{֐.׃applet在浏览器中运行时的权限是受限?不能Ҏ地文件进行读?但是如果applet所在的jar文g如果是经q数字签?q且用户信Q该签?那此applet则可Ҏ地文件进行读?下面l出一个写本地文g的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">" 成功创徏文gc:\\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>此文g~译后生成在com\applet生成两个class文g,用jar打包:</p> <p class="table-info">E:\sylilzy\documents\project\tjava\classes>jar -cvf test.jar com<br />标明清单(manifest)<br />增加Qcom/(d= 0) (写出= 0)(存储?0%)<br />...</p> <p>会发现在当前目录下会生成一?test.jar"文g,此文gcopyCmystore同一个目?准备对该文gq行电子{֐.</p> <p class="table-info">E:\temp>jarsigner -keystore mystore test.jar mykey<br />输入密钥库的口o短语Q?111111<br />警告Q?{֐者证书将在六个月内过期?/p> <p>则test.jar已经被签?~码一个简单的html文g对其q行讉K,html如下:</p> <pre> <font size="2"> Created with Colorer-take5 Library. Type 'html' <span style="COLOR: #a65700"><</span><span style="FONT-WEIGHT: bold; COLOR: #800000">html</span><span style="COLOR: #a65700">></span><span style="COLOR: #a65700"><</span><span style="FONT-WEIGHT: bold; COLOR: #800000">head</span><span style="COLOR: #a65700">></span><span style="COLOR: #a65700"><</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">></span><span style="COLOR: #a65700"><</span><span style="FONT-WEIGHT: bold; COLOR: #800000">title</span><span style="COLOR: #a65700">></span> HTML Test Page <span style="COLOR: #a65700"></</span><span style="FONT-WEIGHT: bold; COLOR: #800000">title</span><span style="COLOR: #a65700">></span><span style="COLOR: #a65700"></</span><span style="FONT-WEIGHT: bold; COLOR: #800000">head</span><span style="COLOR: #a65700">></span><span style="COLOR: #a65700"><</span><span style="FONT-WEIGHT: bold; COLOR: #800000">body</span><span style="COLOR: #a65700">></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"><</span><span style="FONT-WEIGHT: bold; COLOR: #800000">br</span><span style="COLOR: #a65700">></span><span style="COLOR: #a65700"><</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">></span><span style="COLOR: #a65700"></</span><span style="FONT-WEIGHT: bold; COLOR: #800000">applet</span><span style="COLOR: #a65700">></span><span style="COLOR: #a65700"></</span><span style="FONT-WEIGHT: bold; COLOR: #800000">body</span><span style="COLOR: #a65700">></span><span style="COLOR: #a65700"></</span><span style="FONT-WEIGHT: bold; COLOR: #800000">html</span><span style="COLOR: #a65700">></span></font> </pre> <p> </p> <p>以上html文g与test.jar文g攑֜同一目录,用IE打开,则可看到IE弹出安全警告: The application's digital signature is invalid.Do you want to run the application? <br /><br />用户可查看test.jar的签名信?如果选择取消?然后点击create a file 按钮,则提C?access denied(java.io.FilePermission c:\a.txt write) </p> <p>如果在IE弹出安全警告选择"始终信Q此发行者的内容",然后点击q行,再点击create a file 按钮,则提C?成功创徏文gc:\a.txt</p> <p>上面ltest.jar{֐的数字证书是未经验证?现在我们使用从网上申Ll过验证的数字证书给test.jar{֐,看会出现什么结?</p> <p class="table-info">E:\temp>jarsigner -keystore mystore test.jar sily<br />输入密钥库的口o短语Q?111111<br />jarsignerQ?找不?sily 的证书链。sily 必须引用包含专用密钥和相应的公共密钥证书铄有效密钥库密钥条目?/p> <p>q是因ؓsily的证书链不存在导致的,我们需要导入sily的证书链才可形成完整的sily的证书链,可以通过keytool -certreq生成证书铄PKCS#10格式的请?然后再导入请求的PKCS#7格式的回复即?׃名ؓsily的key是从cer文g导入?它在keystore中是以trusted certificate entriescd存储?没有U钥,所以不能生成证书链的请?我们可以前面名为mykey的key导出证书h,或者导出mykey的证?然后此证书安装?受信ȝ根证书颁发机?,然后讉K用此证书{֐q的文g,则IE不再弹出安全警告窗?</p> </span> <hr /> <span style="COLOR: #000000"> <p> </p> <h3>公告</h3> <p> <br />参考资料:<br /><a >http://www.qqread.com/net-saft/z532114081.html</a> 电子{֐的技术实?br /><a >http://www.qqread.com/java/w292768600.html</a> JAVAҎ字证书的常用操作<br /><a >http://java.sun.com/j2se/1.5.0/docs/index.html</a> JDK 5.0 Documentation </p> <p> </p> <p> </p> <fieldset> <legend>作者简?/legend>施祖阻I|名sylilzy,1979q生?br />2002qv从事软g开发工作,主要研究JAVA、Linux及相x术?br />你可通过<a href="mailto:sylilzy@163.com">sylilzy@163.com</a>与作者联pR?</fieldset> <p> </p> </span> <img src ="http://www.aygfsteel.com/sylilzy/aggbug/56929.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/sylilzy/" target="_blank">sylilzy</a> 2006-07-06 12:26 <a href="http://www.aygfsteel.com/sylilzy/articles/JavaAdnDigtalSign.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring的事务处理详?调用一个方法前的事务处理过E?源代码分?http://www.aygfsteel.com/sylilzy/articles/52822.htmlsylilzysylilzyWed, 14 Jun 2006 10:34:00 GMThttp://www.aygfsteel.com/sylilzy/articles/52822.htmlhttp://www.aygfsteel.com/sylilzy/comments/52822.htmlhttp://www.aygfsteel.com/sylilzy/articles/52822.html#Feedback0http://www.aygfsteel.com/sylilzy/comments/commentRss/52822.htmlhttp://www.aygfsteel.com/sylilzy/services/trackbacks/52822.html阅读全文

sylilzy 2006-06-14 18:34 发表评论
]]>
spring的事务处理详?调用一个方法后的事务处理过E?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/sylilzy/articles/52823.html</guid><wfw:comment>http://www.aygfsteel.com/sylilzy/comments/52823.html</wfw:comment><comments>http://www.aygfsteel.com/sylilzy/articles/52823.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/sylilzy/comments/commentRss/52823.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/sylilzy/services/trackbacks/52823.html</trackback:ping><description><![CDATA[ <div> <div> <font color="#000080"> <strong> <div align="center"> <font size="2">spring的事务处理详?调用一个方法后的事务处理过E?/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 id="wmqeeuq" class="diaryTitle" align="center"> <font style="BACKGROUND-COLOR: #0000ff" color="#ffffff" size="2">版权所?转蝲h明出?/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调用一个方法后的事务处理过E?</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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/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.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />   doFinally(txInfo);<br /></span> <span style="COLOR: #008080">14</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/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.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />  doCommitTransactionAfterReturning(txInfo);<br /></span> <span style="COLOR: #008080">16</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/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.aygfsteel.com/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">    施祖阻I|名sylilzy,1979q生?/font> </div> <div> <font style="BACKGROUND-COLOR: #ffffff" color="#000000" size="2">    2002qv从事软g开发工作,主要研究为JAVA、Linux及相x术?/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">与作者联pR?/font> </div> </div> </font> </div> <img src ="http://www.aygfsteel.com/sylilzy/aggbug/52823.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/sylilzy/" target="_blank">sylilzy</a> 2006-06-14 18:34 <a href="http://www.aygfsteel.com/sylilzy/articles/52823.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring的事务处理详?事务创徏http://www.aygfsteel.com/sylilzy/articles/52810.htmlsylilzysylilzyWed, 14 Jun 2006 10:01:00 GMThttp://www.aygfsteel.com/sylilzy/articles/52810.htmlhttp://www.aygfsteel.com/sylilzy/comments/52810.htmlhttp://www.aygfsteel.com/sylilzy/articles/52810.html#Feedback0http://www.aygfsteel.com/sylilzy/comments/commentRss/52810.htmlhttp://www.aygfsteel.com/sylilzy/services/trackbacks/52810.html spring的事务处理详?事务创徏
 
sylilzy@163.com
 
在配|spring的事务处理时,无论使用TransactionProxyFactoryBean,q是使用BeanNameAutoProxyCreator
spring的事务处理都是主要由TransactionInterceptor来拦截完?此类扩展自org.aopalliance.intercept.MethodInterceptor,要查看spring的事务处理过E?首先要了?/font>
TransactionInterceptorcȝ执行q程:
 
 
 
1.事务拦截器拦截调用方?invoke();
2.调用TransactionAspectSupport的createTransactionIfNecessary,
 
 
createTransactionIfNecessary?
首先创徏TransactionInfo对象,然后
如果被调用的Ҏ讄了事务属?不管是什么属?只要讄?,输出日志:
 
TransactionInterceptor 221 - Getting transaction for ...
 
q调用TransactionManager.getTransactionҎ,
 
如果被调用的Ҏ讄未设备事?输出:Don't need to create transaction for ...
 
调用q回后然后将事务l定到当前线E?createTransactionIfNecessaryҎq回.
 
3.调用目标cȝҎ,q依ơ完成其它拦截器的调?/div>
4.如果在上一步操作中有异常抛?则会处理异常,处理q程:Ҏ配置军_是提交还是回滚事?/div>
5.如无异常,调用doFinally()上一个事?旧事?讄为当前事?/div>
4.调用doCommitTransactionAfterReturning提交事务,此ؓ最重要的一?与事务相关的操作在此实际生效.
 
调用TransactionManager.getTransactionq程 :
1.调用doGetTransaction()查找事务(此对象ƈ不代码一个事务已l存?,q回的对象中包含事务的相关信?如事务是否开始等.此对象将在以后作为doBegin and doCommit{方法的参数.
 
2.输出日志:
Using transaction object...
?
HibernateTransactionManager 254 - Using transaction object...
 
3.调用isExistingTransaction(...)查事务是否存?是否已经开始一个事?,此方法ؓabstractҎ,需要concreatecL实现,例如hibernate
 
4.如果isExistingTransaction为true,
如果是PROPAGATION_NEVER,则抛异常
PROPAGATION_NOT_SUPPORTED,则suspend当前事务q返?/div>
PROPAGATION_REQUIRES_NEW,则suspend后创Z个新事务,
其它?/div>
输出日志:"Participating in existing transaction"
然后
处理完后q回一个TransactionStatus对象,包含是否为新transaction,是否为新的newSynchronization,suspendedResources{?getTransaction()同时也返?/font>
 
5. 如果isExistingTransaction为false
查超时是否小于默认时?如果是则抛异?/div>
如果当前Ҏ的事务属性ؓPROPAGATION_MANDATORY,则抛异常,否则
如果当前Ҏ的事务属性ؓPROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED
输出日志:"Creating new transaction with name ..."
调用doBegin(...)创徏q开始一个事?然后q回
否则q回的return newTransactionStatus(definition, null, false, newSynchronization, debugEnabled, null);

作者简介:
    施祖阻I|名sylilzy,1979q生?/font>
    2002qv从事软g开发工作,主要研究为JAVA、Linux及相x术?/font>
    你可通过sylilzy@163.com与作者联pR?/font>



sylilzy 2006-06-14 18:01 发表评论
]]>JNDI全攻略之(?http://www.aygfsteel.com/sylilzy/articles/jndiOfTwo.htmlsylilzysylilzyMon, 19 Dec 2005 10:11:00 GMThttp://www.aygfsteel.com/sylilzy/articles/jndiOfTwo.htmlhttp://www.aygfsteel.com/sylilzy/comments/24662.htmlhttp://www.aygfsteel.com/sylilzy/articles/jndiOfTwo.html#Feedback0http://www.aygfsteel.com/sylilzy/comments/commentRss/24662.htmlhttp://www.aygfsteel.com/sylilzy/services/trackbacks/24662.html
摘要Q本文详l介l了JNDI的目录相兛_容,q以DNS Service ProviderZq行了示例代码的演示.本文为系列文章的W二,JNDI的基内容误本系列的W一?
  阅读全文

sylilzy 2005-12-19 18:11 发表评论
]]>
Java5新特?泛型http://www.aygfsteel.com/sylilzy/articles/Generic.htmlsylilzysylilzyMon, 19 Dec 2005 10:09:00 GMThttp://www.aygfsteel.com/sylilzy/articles/Generic.htmlhttp://www.aygfsteel.com/sylilzy/comments/24661.htmlhttp://www.aygfsteel.com/sylilzy/articles/Generic.html#Feedback0http://www.aygfsteel.com/sylilzy/comments/commentRss/24661.htmlhttp://www.aygfsteel.com/sylilzy/services/trackbacks/24661.html

Java5新特?泛型

sylilzy@163.com
版权所?转蝲h明出?/FONT>

本文关键?/STRONG>QGenerics 泛型 java泛型

Generic只是提供在编译的时候提供一个类型信息,告诉~译器与某个c?如List)相关的是String型还是Double型等信息Q编译时会检查调用该cM的方法时所用的cd和返回的cd是否与该cȝ关的cd一?~译后的代码q无特别之处.

特别注意:Java中引入泛?/STRONG>只是~译时检查,而且主要是检查调用泛型类的类Q运行时q不??.4版的一?/STRONG>

例如AcMؓ泛型c,Bc调用AcȝҎQ编译时会查Bcȝ调用是否W合cd转换要求.对AcL说泛型类T来说具体Z么类型,是不定的,可以看作是当Object来处理的,而且对T赋予什么对象都会出现warn,但可正常q行.

如:对于泛型c?以下UA)Q?BR>
package com.sily;

import java.util.ArrayList;
import java.util.List;

public class GenericClassA<T> {
    
public T p(T c) {
       
T t = (T) (new Double(23));
        
System.out.println(c + " " + c.getClass()+ " " + t);
        
return   c;
    
}

    
public T d() {
        
T t = (T) (new Double(23));
        
return t;
    
}

    
public List<String> l() {
        
return new ArrayList<String>();
    
}
    
/**
     
* @param args
     
*/
}
 
以下用类(以下UB)Q?/FONT>

package com.sily;

public class Test {
    
/**
     
* @param args
     
*/
    
public static void main(String[] args) {
        
GenericClassA<Double> c = new GenericClassA<Double>();
        
String s = c.p(23125.9);
        Double j = c.p(23125.34);
        GenericClassA<String> e = new GenericClassA<String>();
        String ddd = e.d();
        
GenericClassA<String> d = new GenericClassA<String>();
        
d.p(34.23);
    
}
}

会编译出错,是因为c.p(23125.9);q个Ҏq回的类型与定义的泛型不一?但泛型类中的T t = (T) (new Double(23));只是警告.但可正常q行?/SPAN>
Double j = c.p(23125.34);q句可正常运?
但是,String ddd = e.d();q句~译没问?但运行时会出?因ؓe.d()Ҏ实际q回的是一个Double?如果转换为String肯定出错?如果Ҏ:
Double ddd = e.d();~译时出?因ؓ~译器期望的q回cd是String,赋值给String型肯定编译通不q?/SPAN>

另外Q还有一点,一般都只是Acd义时用到<T>,而Bcd是调用,所以是具体的类型,?lt;String>,<Double>
所以说Q?STRONG>泛型出现的目的只是ؓ了限制调用类B调用A时参数类型不出错的一U方?/STRONG>?/SPAN>

CZ分析

对于调用cB?SPAN style="COLOR: #000000">GenericClassA a=new GenericClassA ();也可看作是当Object处理Q但与GenericClassA<Object> b = new GenericClassA<Object>();不同Q如后者b.p(new Object())不会警告Q但前者a.p(new Object())会警?a的所有以泛型作参数的Ҏ都会警告Q这也是Z?.4版本保持向下兼容

GenericClassA<String >不能强制转换?GenericClassA<Object>
?
GenericClassA<Object> c = new GenericClassA<String>();
会出?/SPAN>

?/FONT>
GenericClassA a = new GenericClassA();
GenericClassA<String> c = new GenericClassA<String>();
a=c;

不会出错,q也是ؓ了与1.4版本保持向下兼容;

GenericClassA<?> a ;
GenericClassA<String> c = new GenericClassA<String>();
a=c;

也不会出?/FONT>

new GenericClassA<?>()不能实例化,因ؓcd不确?调用它的相关Ҏ如a.p("ddd")也会出错Q因为类型不定,但可调用其它Ҏ如a.toString();另外,GenericClassA<?>与GenericClassA<? extends Object>一?/FONT>

再看下面的代?

GenericClassA<String> c = new GenericClassA<String>();
Double dd = (Double)c.d();

会出?因ؓ对于~译器来?c.d();q回的就是Stringcd,但在GenericClassA?
该方法如?
public T d() {
        T t = (T) (new Double(23));
        return t;
    }
所以编译时出错,因ؓ不能~译,所以不能运?应该是可q行?
所以以下代码是正确?/FONT>
GenericClassA<Object> c = new GenericClassA<Object>();
Double dd = (Double)c.d();
实际上是对返回g了强制类型{?/FONT>

另外,在javaE序cȝ一个方法中,如p(Object c),则c可有"aaa"{来作参?则会自动转换为Object ,但在泛型?lt;Object>是不能自动{换的,即GenericClassA<Object> c = new GenericClassA<String>();会出?/FONT>
GenericClassA<?> c = new GenericClassA<String>();
c.p("abc");会出?/FONT>
因ؓ对编译器来说c是一个未知类?可能是Object及其子类)的GenericClassA,有可能是Double的GenericClassA,所以如果p("abc")会出?/FONT>

补充Q?/FONT>
要成功运行以上示例代码,请下载最新版jdkQ?A >http://java.sun.com/j2se/1.5.0/download.jsp
另外QEclipse3.1已经对java5提供的支持,大家可在Eclipse3.1中调试以上代?/FONT>

结:
以上为对java5的新特征——泛型的一点小的见解Q更详细的内容请参考sun的tutorial:

泛型只是java5的新特征之一Q其它特征如变参Q元数据,静态导入,增强循环{会在以后陆l介l,敬请x

补充(2005-12-14):
1.如果在spring的getHibernateTemplate().find(sql)q回的类型ؓList<?>(因ؓcd不确?,那么在多的UnitDAO的getUnitByName()Ҏ中应该返回List<UnitDTO>,q个问题怎么解决?

参考资料:

作者简介:
施祖阻I|名sylilzy,1979q生?/FONT>
2002qv从事软g开发工作,主要研究JAVA、Linux及相x术?/FONT>
你可通过sylilzy@163.com与作者联pR?/FONT>


sylilzy 2005-12-19 18:09 发表评论
]]>JNDI全攻略之(一)http://www.aygfsteel.com/sylilzy/articles/jndiOfOne.htmlsylilzysylilzyMon, 19 Dec 2005 10:04:00 GMThttp://www.aygfsteel.com/sylilzy/articles/jndiOfOne.htmlhttp://www.aygfsteel.com/sylilzy/comments/24660.htmlhttp://www.aygfsteel.com/sylilzy/articles/jndiOfOne.html#Feedback0http://www.aygfsteel.com/sylilzy/comments/commentRss/24660.htmlhttp://www.aygfsteel.com/sylilzy/services/trackbacks/24660.html
本文Z自己的理解,错误之处h出宝贉|见。版权所?转蝲h明出?

JNDI全攻略之(一)

关键字:JNDI,J2EE,JavaQ命名和目录接口QJava Naming and Directory Interface
 
名词解释
    jndi是Java 命名和目录接口(Java Naming and Directory InterfaceQJNDIQ的U?从一开始就一直是 Java 2 q_企业版(JEEQ的核心技术之一。在JMSQJMail,JDBC,EJB{技术中Q就大量应用的这U技术?/FONT>
    
Z么会有jndi
    jndi诞生的理׃乎很单。随着分布式应用的发展Q远E访问对象访问成为常用的Ҏ。虽然说通过Socket{编E手D仍然可实现q程通信Q但按照模式的理论来_仍是有其局限性的。RMI技术,RMI-IIOP技术的产生Qɘq程对象的查找成Z技术焦炏VJNDI技术就应运而生。JNDI技术生后Q就可方便的查找q程或是本地对象?/FONT>
 
JNDI的架构与实现
r_jndiarch.jpg    
JNDI的架构与JDBC的架构非常类?JNDI架构提供了一l标准命名系l的API,q些API在JDK1.3之前是作Z个单独的扩展包jndi.jar(通过q个地址下蝲),q个基础API构徏在与SPI之上。这个API提供如下五个?/FONT>
在应用程序中,我们实际上只使到用以上几个包的中c?具体调用cd通信q程对用h说是透明?
JNDI API提供了访问不同JNDI服务的一个标准的l一的实?其具体实现可׃同的Service Provider来完成。前面讲的ؓW一层JNDI API?
 
最下层为JNDI SPI API及其具体实现?/DIV>
 
图中所列的一些SPI可从http://java.sun.com/products/jndi/downloads/index.html下蝲.
r_jndispi.gif 
?/v:shapetype>包括了几个增强和下面的命?目录服务提供者:
  • LDAP(Lightweight Directory Access Protocol)服务提供?
  • CORBA COSQCommon Object Request Broker Architecture Common Object ServicesQ命名服务提供?nbsp;
  • RMI(Java Remote Method Invocation)注册服务提供?
  • DNS(Domain Name System)服务提供?
  • FSSP(File System Service Provider)文gpȝ服务提供?
  • 其它服务提供?/LI>
 
中间层ؓ命名理层。其功能应该由JNDI SPI来完成。上层ؓJNDI API,q个API包在Java 2 SDK 1.3及以上的版本中已l包括?/DIV>
 
前面讲解的只是作为应用程序客L的架构实?其服务端是由SPI对应的公?厂商来实现的,我们只需服务端的相兛_ClJNDI API可以了,具体调用q程由SPI来完?
 
 
JNDI工作原理
 
下面通过一个示例程序来说明JNDI工作原理(代码解释).
/*
* Created on 2005-3-4
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
package com.
sily.jndi;

import java.io.FileInputStream;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
* @author shizy
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class TestJbossJNDI
{
/**
*
*/
public TestJbossJNDI
() {
super();
// TODO Auto-generated constructor stub
}
public static void main(
String[] args) { try {
Properties env =
new Properties();
//载入jboss的SPI相关参数,包括初始上下文工厂,服务URLQ等{?/FONT>
env.
load(new FileInputStream("jbossJndi.properties"));
env.list(System.out);
//通过JNDI api 初始化上下文
InitialContext ctx =
new javax.naming.InitialContext(env);
System.
out.println("Got context");
//create a subContext
ctx.
createSubcontext("/sylilzy");
ctx.createSubcontext("sylilzy/sily");
//rebind a object
ctx.rebind("sylilzy/sily/a", "I am sily a!");
ctx.rebind("sylilzy/sily/b", "I am sily b!");

//lookup context
Context ctx1=(
Context)ctx.lookup("sylilzy");
Context ctx2=(Context)ctx1.lookup("/sylilzy/sily");
ctx2.bind("/sylilzy/g", "this is g");
//lookup binded object
Object o
;
o=
ctx1.lookup("sily/a");
System.out.println("get object from jndi:"+o);
//rename the object
ctx2.
rename("/sylilzy/g", "g1");
o=ctx2.lookup("g1");
System.out.println("get object from jndi:"+o);

} catch (Exception e
) {
e.
printStackTrace();
}
}
}
l果输出如下:
 
-- listing properties --
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Got context
get object from jndi:I am sily a!
get object from jndi:this is g
 
E序?FONT face="Courier New" color=#0000ff>jbossJndi.properties文g的内容ؓ:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
 
注意:要正运行示例程?请启动jboss,q将jboss的jbossall-client.jar文g攑օclasspath中?/FONT>
 
上述CZE序在jboss服务器的jndi树上建立了几个上下文,qbind了几对象,大家可通过附录中的代码或其它工h?/FONT>
查看l果为:
-----------------------------
/sylilzy/sily
-----------------------------
/sylilzy/sily/b:I am sily b!
/sylilzy/sily/a:I am sily a!
/sylilzy/sily/g1:this is g
-----------------------------
-----------------------------
 
上述E序中,我们的代码只涉及Cjndi APIQ其它细节如初始化jboss jndi的初始上下文Q徏立网l连接,与服务器通信Q对我们来说都是透明的,另外Q我们将jboss jndi的spi包中的类名作为参C入了E序?要访问一个远E对象,我们所做的p么多?/FONT>
 
下面Q再提供一个例子,与上例不同,我们不需要jboss,我们使用sun的FSSP(File System Service Provider)文gpȝ服务提供?注意在这个例子中要用到前面所说的File System Service Provider for the Java Naming and Directory InterfaceTM (JNDI)相关c(下蝲Q?/FONT>
/*
 * Created on 2005-3-1
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.sily.jndi;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.*;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
 * @author shizy
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class JndiTest1 {
	/**
	 * 
	 */
	public JndiTest1() {
		super();
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) {
		try {
			Properties env = new Properties();
			env.load(new FileInputStream("fileSystemService.properties"));
			env.put(Context.PROVIDER_URL, "file:///c:/");
			Context ctx = new InitialContext(env);
			ctx.createSubcontext("sylilzy");
			
			NamingEnumeration list = ctx.list("/");
			while (list.hasMore()) {
				NameClassPair nc = (NameClassPair) list.next();
				System.out.println(nc);
			}
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
上例中fileSystemService.properties文g的内容ؓQjava.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
 
q个例子较简单,q行后,它会列出C:\下所有的文g和目录,另外你会发现有一个新目录被创Z.本例不同于上例,它ƈ不需要服务端Q因为它讉K的是文gpȝ.有关帮助可查阅包内的相关文档?/FONT>
 
通过Ҏq两个例子,应该JNDI的工作原理有了一个大致的了解?/FONT>
ȝQ?/FONT>
jndi技术体C分布式应用的优点Q同q它的生也为分布式对象提供了统一的访问接口。由于篇q所限,对目录的操作本文未作介绍Q其它内容将在接下来的系列中讨论。要对JNDI技术作全面的了解,请参阅参考资?要对于JNDI技术深入学习,仍有许多地方值得q一步了解,例如EJB容器所使用的JNDI所提供的对象就?Local和Remote之分Q对于Local Object,对于不同的JVM是不可访问的Q对于远E对象的讉KQ还涉及到Java安全机制?/FONT>
 
附录:
查看jboss jndi内容的代?
//----------------------------------------
/*
 * Created on 2005-3-4
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.sily.jndi;
import java.io.FileInputStream;
import java.util.Properties;

import javax.naming.*;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
 * @author shizy
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ListJbossJndi {
	/**
	 * 
	 */
	public ListJbossJndi() {
		super();
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) {
		try {
			Properties env = new Properties();
			env.load(new FileInputStream("jbossJndi.properties"));
			//env.list(System.out);
			Context ctx = new InitialContext(env);
			listCtx(ctx.lookup("sylilzy"));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	static void listCtx(Object o){
		if(!(o instanceof Context))log(":"+o);
		else {
			log("\n-----------------------------");
		try {
			Context ctx=(Context)o;
			//log(ctx.getNameInNamespace()+"/:");
			NamingEnumeration list=ctx.listBindings("");
			while(list.hasMore()){
				Binding bind=(Binding)list.next();
				log("\n/"+ctx.getNameInNamespace()+"/"+bind.getName());
				listCtx(bind.getObject());
			}
			log("\n-----------------------------");
		}
		catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}
	static void log(Object o){
		System.out.print(o);
	}
}

    
 

作者简介:
    施祖阻I|名sylilzy,1979q生?/FONT>
    2002qv从事软g开发工作,主要研究为JAVA、Linux及相x术?/FONT>
    你可通过sylilzy@163.com与作者联pR?/FONT>
 
参考资料:
 


sylilzy 2005-12-19 18:04 发表评论
]]> վ֩ģ壺 ɽ| | | | | | ƽ| ϻ| ̨| | մ| γ| | ̨| ֶ| ɽ| | | Ž| | | | ѷ| ˳ƽ| | | Զ| | | ̩| ڰ| | °| | | | | Դ| | ¡| |