??xml version="1.0" encoding="utf-8" standalone="yes"?>久久久久亚洲AV成人网,亚洲精品私拍国产福利在线,久久精品国产亚洲AV无码麻豆 http://www.tkk7.com/aoneany/category/39245.htmlƲ上天堂Q先下地?/description>zh-cnFri, 02 Aug 2013 23:49:11 GMTFri, 02 Aug 2013 23:49:11 GMT60~写bat脚本执行jarhttp://www.tkk7.com/aoneany/articles/402292.html沙漠中的?/dc:creator>沙漠中的?/author>Fri, 02 Aug 2013 03:08:00 GMThttp://www.tkk7.com/aoneany/articles/402292.htmlhttp://www.tkk7.com/aoneany/comments/402292.htmlhttp://www.tkk7.com/aoneany/articles/402292.html#Feedback0http://www.tkk7.com/aoneany/comments/commentRss/402292.htmlhttp://www.tkk7.com/aoneany/services/trackbacks/402292.html@echo off       
set PATH=C:\Program Files (x86)\Java\jdk1.6.0_20\bin;C:\WINDOWS;C:\WINDOWS\COMMAND 
set classpath=.;C:\Program Files (x86)\Java\jdk1.6.0_20\lib\tools.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\lib\dt.jar 
java -jar extract_excel.jar
   
pause 


]]>
在CXF中如何传递抽象对?/title><link>http://www.tkk7.com/aoneany/articles/371010.html</link><dc:creator>沙漠中的?/dc:creator><author>沙漠中的?/author><pubDate>Wed, 29 Feb 2012 08:29:00 GMT</pubDate><guid>http://www.tkk7.com/aoneany/articles/371010.html</guid><wfw:comment>http://www.tkk7.com/aoneany/comments/371010.html</wfw:comment><comments>http://www.tkk7.com/aoneany/articles/371010.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/aoneany/comments/commentRss/371010.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/aoneany/services/trackbacks/371010.html</trackback:ping><description><![CDATA[先定一个适配器类Q承XmlAdapter,可以抽象类转换为可以识别的传输对象<br /> <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> UserTOAdapter </span><span style="color: #0000FF; ">extends</span><span style="color: #000000; "> XmlAdapter</span><span style="color: #000000; "><</span><span style="color: #000000; ">TO, User</span><span style="color: #000000; ">></span><span style="color: #000000; "> {<br /> <br />     @Override<br />     </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> User unmarshal(TO to) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception {<br />         </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> (User)to;<br />     }<br /> <br />     @Override<br />     </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> TO marshal(User user) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception {<br />         </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> (TO)user;<br />     }<br /> <br /> }<br /> </span></div> <div>在WebService接口中定义参数{换的ҎQ如?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">@WebService<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">interface</span><span style="color: #000000; "> TypeConvert {<br />    @WebMethod<br />    @XmlJavaTypeAdapter(UserTOAdapter.</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">)<br />    User addUser(User user);<br />}</span></div><br /><br /></div><img src ="http://www.tkk7.com/aoneany/aggbug/371010.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/aoneany/" target="_blank">沙漠中的?/a> 2012-02-29 16:29 <a href="http://www.tkk7.com/aoneany/articles/371010.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>位移q制q算http://www.tkk7.com/aoneany/articles/348889.html沙漠中的?/dc:creator>沙漠中的?/author>Sat, 23 Apr 2011 13:58:00 GMThttp://www.tkk7.com/aoneany/articles/348889.htmlhttp://www.tkk7.com/aoneany/comments/348889.htmlhttp://www.tkk7.com/aoneany/articles/348889.html#Feedback0http://www.tkk7.com/aoneany/comments/commentRss/348889.htmlhttp://www.tkk7.com/aoneany/services/trackbacks/348889.html带符号右U?题:Q?5 >> 2 = -4

15原码:   00000000 00000000 00000000 00001111 //32?二进?br /> 反码Q?   11111111 11111111 11111111 11110000 //0?,1变O
补码Q?   11111111 11111111 11111111 11110001 //最后位?,-15二进?br /> 右移2位:  11111111 11111111 11111111 11111100 //双丢弃2?前面30位保?左边?
取反Q?nbsp;     00000000 00000000 00000000 00000011 //0?,1变O
Q1:                                       3+1
l果:                                     Q-Q?//负号保留,十进?/p>


带符号左U?? 10 << 2 = 40
10 补码:    00000000 00000000 00000000 00001010 //32?二进?br /> 左移2?    00000000 00000000 00000000 00101000 //左边丢弃2?双?
l果:                                        40 //十进?/p>

 

无符号右U?题:Q?321 >>> 30 = 3
4321原码:         00000000 00000000 00010000 11100011 //32?二进?br /> 反码Q?          11111111 11111111 11101111 00011100 //0?,1变O
补码Q?          11111111 11111111 11101111 00011101 //最后位?,-4321二进?br /> 无符号右U?0位:  00000000 00000000 00000000 00000011 //双丢弃30?前面二位保留,左边?
l果:                                                3 //十进?/p>


& 位逻辑?题:44 & 21 = 4
44 补码:    00000000 00000000 00000000 00101100 //32?二进?br /> 21 补码:    00000000 00000000 00000000 00010101 //32?二进?br /> & q算:     00000000 00000000 00000000 00000100 //对应的两个二q制位均??l果位才? 否则?
l果:                                         4 //十进?nbsp;  
                                

| 位逻辑?题:9 | 5 = 13
9 补码:    00000000 00000000 00000000 00001001 //32?二进?br /> 5 补码:    00000000 00000000 00000000 00000101 //32?二进?br /> | q算:    00000000 00000000 00000000 00001101 //对应的二个二q制位有一个ؓ1Ӟl果位就?
l果:                                       13 //十进?


^ 位逻辑异或 ? 9 ^ 5 = 12
9 补码:    00000000 00000000 00000000 00001001 //32?二进?br /> 5 补码:    00000000 00000000 00000000 00000101 //32?二进?br /> | q算:    00000000 00000000 00000000 00001100 //对应的二q制位相异时Q结果ؓ1
l果:                                       12 //十进?/p>


~ 位逻辑?? ~9 = -10
9 补码:    00000000 00000000 00000000 00001001 //32?二进?br /> ~ q算:    11111111 11111111 11111111 11110110 //最高位?表示Z个负?则进行取反加1
取反:      00000000 00000000 00000000 00001001 //32?二进?br /> Q1:                                      9+1 //32?二进?br /> l果:                                      -10 //十进?/p>


在计机pȝ中,数g律用补码来表C(存储Q?br /> 主要原因Q用补码,可以符号位和其它位l一处理Q同Ӟ减法也可按加法来处理。另外,两个用补
码表C的数相加时Q如果最高位Q符号位Q有q位Q则q位被舍弃?br /> 补码与原码的转换q程几乎是相同的?br /> 数值的补码表示也分两种情况Q?br /> Q?Q正数的补码Q与原码相同?br />       例如Q?9的补码是00001001?br /> Q?Q负数的补码Q符号位?Q其余位数绝对值的原码按位取反Q然后整个数??br />       例如Q?7的补码:因ؓ是负敎ͼ则符号位?#8220;1”,整个?0000111Q其?位ؓ-7的绝对?7的原?br />      0000111按位取反?111000Q再?Q所?7的补码是11111001?br /> 已知一个数的补码,求原码的操作分两U情况:
Q?Q如果补码的W号位ؓ“0”Q表C是一个正敎ͼ所以补码就是该数的原码?br /> Q?Q如果补码的W号位ؓ“1”Q表C是一个负敎ͼ求原码的操作可以是:W号位ؓ1Q其余各位取反,然后再整个数??br />      例如Q已知一个补码ؓ11111001Q则原码?0000111Q?7Q:因ؓW号位ؓ“1”Q表C是一个负敎ͼ所以该位不变,仍ؓ“1”Q其??111001取反后ؓ0000110Q再?Q所以是10000111?

 

转蝲Q?a >http://blog.csdn.net/zjp8023/archive/2009/05/29/4224121.aspx



]]>
您的数点到哪里Mhttp://www.tkk7.com/aoneany/articles/348753.html沙漠中的?/dc:creator>沙漠中的?/author>Thu, 21 Apr 2011 15:36:00 GMThttp://www.tkk7.com/aoneany/articles/348753.htmlhttp://www.tkk7.com/aoneany/comments/348753.htmlhttp://www.tkk7.com/aoneany/articles/348753.html#Feedback0http://www.tkk7.com/aoneany/comments/commentRss/348753.htmlhttp://www.tkk7.com/aoneany/services/trackbacks/348753.htmlJava 语言支持两种基本的Q点类型: float ?double Q以及与它们对应的包装类 Float ?Double 。它们都依据 IEEE 754 标准Q该标准?32 位Q点和 64 位双_ֺ点二进制小数定义了二进制标准?

IEEE 754 用科学记数法以底Cؓ 2 的小数来表示点数。IEEE 点数用 1 位表C数字的W号Q用 8 位来表示指数Q用 23 位来表示数Q即数部分。作为有W号整数的指数可以有正负之分。小数部分用二进Ӟ底数 2Q小数来表示Q这意味着最高位对应着??(2 -1)Q第二位对应着 ?(2 -2)Q依此类推。对于双_ֺ点敎ͼ?11 位表C指敎ͼ52 位表C尾数。IEEE 点值的格式如图 1 所C?


?1. IEEE 754 点数的格式
?1. IEEE 754 点数的格式

因ؓ用科学记数法可以有多U方式来表示l定数字Q所以要规范化QҎQ以便用底数?2 q且数点左边ؓ 1 的小数来表示Q按照需要调节指数就可以得到所需的数字。所以,例如Q数 1.25 可以表示为尾Cؓ 1.01Q指Cؓ 0Q?(-1) 0*1.01 2*2 0

?10.0 可以表示为尾Cؓ 1.01Q指Cؓ 3Q?(-1) 0*1.01 2*2 3

 

一个十q制数能否用二进制QҎ_表示Q关键在于小数部分。我们来看一个最单的数Q{QIEEE754能否_表示。按照乘?取整C的方法,有:

Q{QIEEE754

得到一个无限@环的二进制小?img title="Q{QIEEE754" border="0" alt="Q{QIEEE754" src="http://www.cnblogs.com/images/cnblogs_com/bossin/WindowsLiveWriter/IEEE754_B489/clip_image020.gif" width="177" height="24" real_src="http://www.cnblogs.com/images/cnblogs_com/bossin/WindowsLiveWriter/IEEE754_B489/clip_image020.gif" />Q用有限位无法表C无限@环小敎ͼ因此Q?img title="Q{QIEEE754" border="0" alt="Q{QIEEE754" src="http://www.cnblogs.com/images/cnblogs_com/bossin/WindowsLiveWriter/IEEE754_B489/clip_image016%5B1%5D.gif" width="44" height="24" real_src="http://www.cnblogs.com/images/cnblogs_com/bossin/WindowsLiveWriter/IEEE754_B489/clip_image016%5B1%5D.gif" />无法用IEEE 754点数精表C。从中也可以看到Q由?/p>

Q{QIEEE754Q?/p>

q四个数也无法精表C。同理:

Q{QIEEE754

也无法用IEEE 754点数精表C?/p>

在以0.1~0.9l尾?个小CQ只?.5可以_表示Q(如)Q{QIEEE754Q而其他均无法q行_转换?/p>

]]>
遍历查找性能试报告QMap,Array,RBTreeQ?/title><link>http://www.tkk7.com/aoneany/articles/348346.html</link><dc:creator>沙漠中的?/dc:creator><author>沙漠中的?/author><pubDate>Fri, 15 Apr 2011 05:39:00 GMT</pubDate><guid>http://www.tkk7.com/aoneany/articles/348346.html</guid><wfw:comment>http://www.tkk7.com/aoneany/comments/348346.html</wfw:comment><comments>http://www.tkk7.com/aoneany/articles/348346.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/aoneany/comments/commentRss/348346.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/aoneany/services/trackbacks/348346.html</trackback:ping><description><![CDATA[  <p><span style="font-family: 宋体">  </p> <p><span style="font-family: 宋体">上次只测试查询的性能Q而且都是在最优的情况下进行,没有考虑条他情况Q这ơ重C改了试用例Q把整个试的各个过E描q出来(加蝲Q排序,查找的效率)?/span></p> <p><strong><span style="font-family: 宋体">场景Q?/span></strong><span style="font-family: 宋体">随机生成</span>5<span style="font-family: 宋体">百万条不同的记录Q而每条记录不存在相同的情c现在将q?/span>5<span style="font-family: 宋体">百万条记录在不同的情况下q行试。测试结果如?/span></p> <p>1<span style="font-family: 宋体">?/span>5<span style="font-family: 宋体">百万条记录,d查询</span>50<span style="font-family: 宋体">万次Q查询前</span>50<span style="font-family: 宋体">万条记录Q?/span></p> <p><span style="font-family: 宋体">数组性能试加蝲p</span>:<strong><span style="color: red">6.11</strong></span><span style="font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">数组性能试排序p</span>:<strong><span style="color: red">18.313</strong></span><span style="font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">数组性能试查找p</span>:<strong><span style="color: red">3.297</strong></span><span style="font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">数组性能试p?/span>:<strong><span style="color: red">27.72</strong></span><span style="font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">数组性能试d?/span>:<strong><span style="color: red">100005888</strong> </span><span style="font-family: 宋体">字节</span></p> <p>RBTree<span style="font-family: 宋体">性能试加蝲p</span>:<strong><span style="color: red">53.644</strong></span><span style="font-family: 宋体">U?/span></p> <p>RBTree<span style="font-family: 宋体">性能试查找p?/span>:<strong><span style="color: red">2.86</strong></span><span style="font-family: 宋体">U?/span></p> <p>RBTree<span style="font-family: 宋体">性能试p?/span>:<strong><span style="color: red">56.504</strong></span><span style="font-family: 宋体">U?/span></p> <p>RBTREE<span style="font-family: 宋体">性能试共占?/span>:<strong><span style="color: red">240004864</strong> </span><span style="font-family: 宋体">字节</span></p> <p>MAP<span style="font-family: 宋体">性能试加蝲p</span>:<strong><span style="color: red">7.282</strong></span><span style="font-family: 宋体">U?/span></p> <p>MAP<span style="font-family: 宋体">性能试查找p</span>:<strong><span style="color: red">0.11</strong></span><span style="font-family: 宋体">U?/span></p> <p>MAP<span style="font-family: 宋体">性能试p?/span>:<strong><span style="color: red">7.392</strong></span><span style="font-family: 宋体">U?/span></p> <p>MAP<span style="font-family: 宋体">性能试</span> <span style="font-family: 宋体">共占?/span>:<strong><span style="color: red">173554048</strong> </span><span style="font-family: 宋体">字节</span></p> <p>2<span style="font-family: 宋体">?/span>5<span style="font-family: 宋体">百万条记录,d查询</span>5<span style="font-family: 宋体">百万ơ(每条记录查询一ơ)</span></p> <p><span style="font-family: 宋体">数组性能试加蝲p</span>:<strong><span style="color: red">6.078</strong></span><span style="font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">数组性能试排序p</span>:<strong><span style="color: red">17.642</strong></span><span style="font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">数组性能试查找p</span>:<strong><span style="color: red">33.049</strong></span><span style="font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">数组性能试p?/span>:<strong><span style="color: red">56.769</strong></span><span style="font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">数组性能试d占用</span> = <strong><span style="color: red">100005552</strong> </span><span style="font-family: 宋体">字节</span></p> <p>RBTree<span style="font-family: 宋体">性能试加蝲p</span>:<strong><span style="color: red">54.8</strong></span><span style="font-family: 宋体">U?/span></p> <p>RBTree<span style="font-family: 宋体">性能试查找p?/span>:<strong><span style="color: red">34.581</strong></span><span style="font-family: 宋体">U?/span></p> <p>RBTree<span style="font-family: 宋体">性能试p?/span>:<strong><span style="color: red">89.381</strong></span><span style="font-family: 宋体">U?/span></p> <p>RBTREE<span style="font-family: 宋体">性能试共占?/span> = <strong><span style="color: red">240005024</strong> </span><span style="font-family: 宋体">字节</span></p> <p>MAP<span style="font-family: 宋体">性能试加蝲p</span>:<strong><span style="color: red">6.501</strong></span><span style="font-family: 宋体">U?/span></p> <p>MAP<span style="font-family: 宋体">性能试查找p</span>:<strong><span style="color: red">0.937</strong></span><span style="font-family: 宋体">U?/span></p> <p>MAP<span style="font-family: 宋体">性能试p?/span>:<strong><span style="color: red">7.438</strong></span><span style="font-family: 宋体">U?/span></p> <p>MAP<span style="font-family: 宋体">性能试</span> <span style="font-family: 宋体">共占?/span>=<strong><span style="color: red">173553888</strong> </span><span style="font-family: 宋体">字节</span></p> <p>3<span style="font-family: 宋体">?/span>5<span style="font-family: 宋体">百万条记录,d查询</span>5<span style="font-family: 宋体">千万ơ(每条数据查询</span>10<span style="font-family: 宋体">ơ)</span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试加蝲p</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">6.11</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试排序p</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">17.861</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试查找p</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">331.224</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试p?/span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">355.195</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试d占用</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">99990456</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">字节</span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">RBTree</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试加蝲p</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">53.097</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">RBTree</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试查找p?/span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">345.866</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">RBTree</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试p?/span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">398.963</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">RBTREE</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试共占?/span><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> = </span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">240005248 </span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">字节</span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">MAP</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试加蝲p</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">7.235</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">MAP</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试查找p</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">9.375</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">MAP</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试p?/span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">16.61</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">MAP</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试</span><span style="font-size: 10pt; color: black; font-family: 宋体">共占?/span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">173554048</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">字节<br /> <br /> 试文g地址Q?nbsp; <a href="/Files/aoneany/search2.rar">/Files/aoneany/search2.rar</a><br /> <br /> ================================================================<br />     </p> <p class="MsoNormal"><span style="color: #1f497d; font-family: 宋体">前面的测试算法中Q有一个地方偷懒(直接使用</span><span lang="EN-US" style="color: #1f497d">String.CompareTo</span><span style="color: #1f497d; font-family: 宋体">Ҏq行比较Q导致查询效率很差,现在对其q行优化Q测试后的数据ؓQ?/span><span lang="EN-US" style="color: #1f497d"><o:p></o:p></span></p> <p class="MsoNormal"><span lang="EN-US" style="color: #1f497d"><o:p> </o:p></span></p> <p class="MsoNormal"><span lang="EN-US">5</span><span style="font-family: 宋体">百万条记录,d查询</span><span lang="EN-US">5</span><span style="font-family: 宋体">百万ơ(每条记录查询一ơ)</span><span lang="EN-US" style="color: #1f497d"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试加蝲p</span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<span style="color: red"><strong>1.016</strong></span></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试排序p</span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<span style="color: red"><strong>14.485</strong></span></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试查找p</span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<span style="color: red"><strong>0.703</strong></span></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 宋体">数组性能试p?/span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<strong style="color: red">16.204</strong></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">RBTree</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试加蝲p</span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<strong style="color: red">20.705</strong></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">RBTree</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试查找p?/span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<strong style="color: red">3.75</strong></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">RBTree</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试p?/span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<strong style="color: red">24.455</strong></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">MAP</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试加蝲p</span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<strong style="color: red">3.875</strong></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">MAP</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试查找p</span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<strong style="color: red">1.516</strong></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span><span lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'"><o:p></o:p></span></p> <p class="MsoNormal" style="text-align: left" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">MAP</span><span style="font-size: 10pt; color: black; font-family: 宋体">性能试p?/span><span lang="EN-US" style="font-size: 10pt; color: black; font-family: 'Courier New'">:<strong style="color: red">5.391</strong></span><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> </span></span> <img src ="http://www.tkk7.com/aoneany/aggbug/348346.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/aoneany/" target="_blank">沙漠中的?/a> 2011-04-15 13:39 <a href="http://www.tkk7.com/aoneany/articles/348346.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>遍历查找性能试报告QIf,Switch,Map,Array,RBTreeQ?/title><link>http://www.tkk7.com/aoneany/articles/348129.html</link><dc:creator>沙漠中的?/dc:creator><author>沙漠中的?/author><pubDate>Tue, 12 Apr 2011 04:01:00 GMT</pubDate><guid>http://www.tkk7.com/aoneany/articles/348129.html</guid><wfw:comment>http://www.tkk7.com/aoneany/comments/348129.html</wfw:comment><comments>http://www.tkk7.com/aoneany/articles/348129.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/aoneany/comments/commentRss/348129.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/aoneany/services/trackbacks/348129.html</trackback:ping><description><![CDATA[  <span style="font-family: 宋体">性能试报告</span> <p><span style="font-family: 宋体">场景一</span></p> <p><span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">Q?/span>2<span style="font-family: 宋体">Q?/span>3<span style="font-family: 宋体">Q?/span>4<span style="font-family: 宋体">Q?/span>5<span style="font-family: 宋体">个整敎ͼ分别Ҏ个一数遍?/span>2<span style="font-family: 宋体">亿次Qd</span>10<span style="font-family: 宋体">亿次Q在不同用例试p的时间如?/span></p> <p><span style="font-size: 10pt; color: black; font-family: 'Courier New'">Switch</span><span style="font-size: 10pt; color: black; font-family: 宋体">试</span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">dp</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">20.329</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p>IF<span style="font-family: 宋体">试</span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">dp</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">20.437</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p><span style="font-size: 10pt; color: black; font-family: 'Courier New'">Array</span><span style="font-size: 10pt; color: black; font-family: 宋体">试</span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">dp</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">20.703</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p><span style="font-size: 10pt; color: black; font-family: 'Courier New'">Map</span><span style="font-size: 10pt; color: black; font-family: 宋体">试</span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">dp</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">56.719</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p><span style="font-family: 宋体">场景?/span></p> <p><span style="font-family: 宋体">?/span>5<span style="font-family: 宋体">百万条数据,分别Ҏ一个数据访?/span>10<span style="font-family: 宋体">ơ,d查找</span>5<span style="font-family: 宋体">千万ơ,在不同用例测试花费的旉如下Q?/span></p> <p>Map<span style="font-family: 宋体">试</span></p> <p><span style="font-family: 宋体">dpQ?/span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">4.047</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U(自己对这l果很怀疑,但是试几次都是q样Q?/span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">内存占用Q?/span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">273579872</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">字节</span></p> <p><span style="font-size: 10pt; color: black; font-family: 'Courier New'">CPU</span><span style="font-size: 10pt; color: black; font-family: 宋体">占用Q?/span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">50%</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">左右</span></p> <p>Array Comparable<span style="font-size: 10pt; color: black; font-family: 宋体">试</span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">dp</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">: </span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">45.312</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">内存占用Q?/span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">160025376</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">字节</span></p> <p><span style="font-size: 10pt; color: black; font-family: 'Courier New'">CPU</span><span style="font-size: 10pt; color: black; font-family: 宋体">占用Q?/span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">50%</strong></span><span style="font-size: 10pt; color: black; font-family: 宋体">左右</span></p> <p><span style="font-size: 10pt; color: black; font-family: 'Courier New'">RBTree</span><span style="font-size: 10pt; color: black; font-family: 宋体">试</span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">dpQ?/span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">63.485</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">U?/span></p> <p><span style="font-size: 10pt; color: black; font-family: 宋体">内存占用Q?/span><span style="font-size: 10pt; color: red; font-family: 'Courier New'">320030976</span><span style="font-size: 10pt; color: black; font-family: 宋体">字节</span></p> <p><span style="font-size: 10pt; color: black; font-family: 'Courier New'">CPU</span><span style="font-size: 10pt; color: black; font-family: 宋体">占用Q?/span><strong><span style="font-size: 10pt; color: red; font-family: 'Courier New'">50%</span></strong><span style="font-size: 10pt; color: black; font-family: 宋体">左右<br /> <br /> <br /> 试文g地址Q?a href="http://www.tkk7.com/Files/aoneany/search.rar">http://www.tkk7.com/Files/aoneany/search.rar</a></span></p> <img src ="http://www.tkk7.com/aoneany/aggbug/348129.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/aoneany/" target="_blank">沙漠中的?/a> 2011-04-12 12:01 <a href="http://www.tkk7.com/aoneany/articles/348129.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>cd信息-Thinking in javaMW记http://www.tkk7.com/aoneany/articles/304119.html沙漠中的?/dc:creator>沙漠中的?/author>Sun, 29 Nov 2009 11:23:00 GMThttp://www.tkk7.com/aoneany/articles/304119.htmlhttp://www.tkk7.com/aoneany/comments/304119.htmlhttp://www.tkk7.com/aoneany/articles/304119.html#Feedback0http://www.tkk7.com/aoneany/comments/commentRss/304119.htmlhttp://www.tkk7.com/aoneany/services/trackbacks/304119.htmlClass.forName("Gum");

forName是取得Class引用的一U方法,q回一个Class对象的引用?/p>

如果Gumc还没有被加载就加蝲它,在加载过E中QGum的静态子句被执行?/p>

可能产生的异常:ClassNotFoundException

Class.getInterfaces("Gum")

q回对象是ClasscdQ表C类Gum包含的接?/p>

如类Gum implements interface1,

则会获取到interface1的类对象

Class.newInstance()

实现“虚拟构造器”的一U途径

 

cd面常?/strong>

obj.Class生成对Class对象的引用,它比forName更加安全Q因为它在编译时接受检查?/p>

注意:当?Class来创建Class对象的引用时Q不会自动初始化Class对象?/p>

class Initable{
    
static final int staticFinal=47;
    
static final int staticFinal2=ClassInitialization.rand.nextInt(1000);
    
static{
        System.out.println(
"Initialzing Initable");
    }

}

class Initable2{
    
static int staticNonFinal=147;
    
static{
        System.out.println(
"Initialzing Initable2");
    }

}

class Initable3{
    
static int staticNonFinal=74;
    
static{
        System.out.println(
"Initialzing Initable3");
    }

}

public class ClassInitialization {
    
public static Random rand=new Random(47);
    
public static void main(String[] args){
        Class initable
=Initable.class;
        System.out.println(
"After creating Initable ref");
        System.out.println(Initable.staticFinal);
        System.out.println(Initable.staticFinal2);
        System.out.println(Initable2.staticNonFinal);
        Class initable3
=Class.forName("Initable3");
        System.out.println(
"After creating Initable3 ref");
        System.out.println(Initable3.staticNonFinal);
    }

}

 

输出l果为:

Initialzing Initable
After creating Initable ref
47
258
Initialzing Initable2
147
Initialzing Initable3
After creating Initable3 ref
74

l论Q?/strong>如果一个static final值是"~译期常?Q就象Initable.staticFinal那样Q那么这个g需要对Initablecd使化可以读取,但是如果只是一个域讄为static和final的,如对Initable.staticFinal2的访问将q行强制的初使化Q因为它不是一个编译型帔R?/p>

如果一个static而不是final的,那么在它讉KӞL要求q行链接Qؓq个域分配存储空_和初始化Q初始化该存储空_Q就像对Initable2.staticNonFinal那样?/p>

泛化的Class引用

Class<Integer> iniClass=int.class

新的转型语法

case()

InstanceOf

反射

getMethods()q回Method对象的数l?/p>

getConstructors()q回Contructor对象的数l?/p>

动态代?/strong>

实现InvocationHandler接口

public Object invoke(Objct proxy,Method method,Object[] args)throws Throwable{}Ҏ

通过Proxy.newProxyInstance(ClassLoader,Class[],InvocationHandler)创徏动态代?/p>

具体CZ参见:

http://www.tkk7.com/aoneany/articles/271019.html



]]>
字符?Thinking in JavaMW记http://www.tkk7.com/aoneany/articles/304020.html沙漠中的?/dc:creator>沙漠中的?/author>Sat, 28 Nov 2009 05:54:00 GMThttp://www.tkk7.com/aoneany/articles/304020.htmlhttp://www.tkk7.com/aoneany/comments/304020.htmlhttp://www.tkk7.com/aoneany/articles/304020.html#Feedback0http://www.tkk7.com/aoneany/comments/commentRss/304020.htmlhttp://www.tkk7.com/aoneany/services/trackbacks/304020.htmlString对象是不可变?/p>

Scanner扫描字符串对象?/p>

备注Q?/p>

在java~译好的class文g?有个区域UCؓConstant Pool,他是一个由数组l成的表,cd
为cp_info constant_pool[],用来存储E序中用的各种帔R,包括Class/String/Integer{各
U基本Java数据cd,详情参见The Java Virtual Machine Specification
4.4章节.

对于Constant Pool,表的基本通用l构?
cp_info {
        u1 tag;
        u1 info[];
}

tag是一个数?用来表示存储的常量的cd,例如8表示Stringcd,5表示Longcd,info[]Ҏ
cd码tag的不同会发生相应变化.
对于Stringcd,表的l构?
CONSTANT_String_info {
        u1 tag;
        u2 string_index;
}
tag固定?,string_index是字W串内容信息,cd?
CONSTANT_Utf8_info {
        u1 tag;
        u2 length;
        u1 bytes[length];
}
tag固定?,length为字W串的长?bytes[length]为字W串的内?

代码样例
(以下代码在jdk6中编?
Z详细理解Constant Pool的结?我们参看一些代?
    String s1
= "sss111";
    String s2
= "sss222";
    System.out.println(s1
+ " " + s2);
׃
"sss111"?/span>"sss222"都是字符串常?在编译期已l创建好了存储在class文g?
在编译后的class文g中会存在q?个常量的对应表示:
08 00 11 01 00 06 73 73 73 31 31 31 08 00 13 01 ; ......sss111....
00 06 73 73 73 32 32 32                         ; ..sss222

Ҏ上面说的String帔Rl构,我们分析一?br /> 开始的08为CONSTANT_String_infol构中的tag,?1应该是它的相对引?01?br /> CONSTANT_Utf8_info的tag,06为对应字W串的长?
73 73 73 31 31 31为字W串?br /> 应的~码,接着分析,会发现后面的是对?/span>"sss222"的存储结?

l过上面分析,我们知道?1?3是两个字W串的相对引?可以修改class文g
来修Ҏ印的内容,把class文g中的
00 6E 00 04 00 03 00 00 00 24 12 10 4C 12 12 4D
Ҏ
00 6E 00 04 00 03 00 00 00 24 12 10 4C 12 10 4D
E序׃输出sss111 sss111,而不是和原程序一栯出sss111 sss222,因ؓ?br /> 们把?/span>"sss222"的相对引?2Ҏ了对"sss111"的相对引?0.

------------分割U?br /> public class Test {
   
public static void main(String[] args) {
        String s1
= "sss111";
        String s2
= "sss111";
    }
}

在上面程序中存在2个相同的帔R
"sss111",对于n个值相同的String帔R,在Constant Pool?br /> 只会创徏一?所以在~译好的class文g?我们只能扑ֈ一个对"sss111"的表C?
000000abh:
08 00 11 01 00 06 73 73 73 31 31 31             ; ......sss111

在程序执行的时?Constant Pool会储存在Method Area,而不是heap?

另外,对于
""内容为空的字W串帔R,会创Z个长度ؓ0,内容为空的字W串攑ֈConstant Pool?
而且Constant Pool在运行期是可以动态扩展的.

关于Stringcȝ说明

1.String使用private final char value[]来实现字W串的存?也就是说String对象创徏之后,׃?br /> 再修Ҏ对象中存储的字符串内?是因ؓ如此,才说Stringcd是不可变?immutable).

2.StringcL一个特D的创徏Ҏ,是使用""双引h创徏.例如new String("i am")实际创徏??br /> String对象,一个是"i am"通过""双引号创建的,另一个是通过new创徏?只不q他们创建的时期不同,
一个是~译?一个是q行?/span>!

3.java对Stringcd重蝲?/span>+操作W?可以直接使用+对两个字W串q行q接.

4.q行期调用Stringcȝintern()Ҏ可以向String Pool中动态添加对?

String的创建方?/strong>

一般有如下几种
1.直接使用""引号创徏.
2.使用new String()创徏.
3.使用new String("someString")创徏以及其他的一些重载构造函数创?
4.使用重蝲的字W串q接操作W?/span>+创徏.

面试?

String s1 = new String("s1") ;
String s2
= new String("s1") ;
上面创徏了几个String对象
?
{案:3?,~译期Constant Pool中创??q行期heap中创??




]]>
最单的web服务器代?Java)http://www.tkk7.com/aoneany/articles/283248.html沙漠中的?/dc:creator>沙漠中的?/author>Fri, 19 Jun 2009 08:36:00 GMThttp://www.tkk7.com/aoneany/articles/283248.htmlhttp://www.tkk7.com/aoneany/comments/283248.htmlhttp://www.tkk7.com/aoneany/articles/283248.html#Feedback0http://www.tkk7.com/aoneany/comments/commentRss/283248.htmlhttp://www.tkk7.com/aoneany/services/trackbacks/283248.html阅读全文

]]>
Java实现一个用户在同一旉只登陆一?/title><link>http://www.tkk7.com/aoneany/articles/272005.html</link><dc:creator>沙漠中的?/dc:creator><author>沙漠中的?/author><pubDate>Thu, 21 May 2009 08:02:00 GMT</pubDate><guid>http://www.tkk7.com/aoneany/articles/272005.html</guid><wfw:comment>http://www.tkk7.com/aoneany/comments/272005.html</wfw:comment><comments>http://www.tkk7.com/aoneany/articles/272005.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/aoneany/comments/commentRss/272005.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/aoneany/services/trackbacks/272005.html</trackback:ping><description><![CDATA[<p>保证|站用户Q或理员)在同一旉只登陆一ơ在很多场合是很必要的!下面举一个基于Javaq_实现的例子?br />     <strong>W一步:创徏SessionListenterc?/strong></p> <p> </p> <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"><img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpSession;<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpSessionEvent;<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpSessionListener;<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000"> org.apache.log4j.Logger;<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000"> org.jpxx.sw.Constants;<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000"> org.jpxx.sw.Factory; <br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /> <img id="Codehighlighter1_222_353_Open_Image" onclick="this.style.display='none'; Codehighlighter1_222_353_Open_Text.style.display='none'; Codehighlighter1_222_353_Closed_Image.style.display='inline'; Codehighlighter1_222_353_Closed_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_222_353_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_222_353_Closed_Text.style.display='none'; Codehighlighter1_222_353_Open_Image.style.display='inline'; Codehighlighter1_222_353_Open_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ContractedBlock.gif" align="top" alt="" /></span><span id="Codehighlighter1_222_353_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">/** */</span><span id="Codehighlighter1_222_353_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" /> * 该监听器用于监听后台理员登陆。只允许l一理员在同一旉登陆一ơ?br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" /> * </span><span style="color: #808080">@author</span><span style="color: #008000"> Jun Li<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" /> * </span><span style="color: #808080">@version</span><span style="color: #008000">  $Revision: 1.0.2 $, $Date: 2009/01/05 $<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" /> * </span><span style="color: #808080">@since</span><span style="color: #008000"> 1.0.0<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" /> </span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /> <img id="Codehighlighter1_415_1053_Open_Image" onclick="this.style.display='none'; Codehighlighter1_415_1053_Open_Text.style.display='none'; Codehighlighter1_415_1053_Closed_Image.style.display='inline'; Codehighlighter1_415_1053_Closed_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_415_1053_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_415_1053_Closed_Text.style.display='none'; Codehighlighter1_415_1053_Open_Image.style.display='inline'; Codehighlighter1_415_1053_Open_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ContractedBlock.gif" align="top" alt="" /></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> SessionListener </span><span style="color: #0000ff">implements</span><span style="color: #000000"> HttpSessionListener </span><span id="Codehighlighter1_415_1053_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.tkk7.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_415_1053_Open_Text"><span style="color: #000000">{ <br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" />    Logger log </span><span style="color: #000000">=</span><span style="color: #000000"> Factory.getSingletonInstance().getLogger(SessionListener.</span><span style="color: #0000ff">class</span><span style="color: #000000">);<br /> <img id="Codehighlighter1_556_562_Open_Image" onclick="this.style.display='none'; Codehighlighter1_556_562_Open_Text.style.display='none'; Codehighlighter1_556_562_Closed_Image.style.display='inline'; Codehighlighter1_556_562_Closed_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_556_562_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_556_562_Closed_Text.style.display='none'; Codehighlighter1_556_562_Open_Image.style.display='inline'; Codehighlighter1_556_562_Open_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" alt="" />    </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> sessionCreated(HttpSessionEvent event) </span><span id="Codehighlighter1_556_562_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.tkk7.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_556_562_Open_Text"><span style="color: #000000">{<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />    }</span></span><span style="color: #000000"> <br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /> <img id="Codehighlighter1_623_1051_Open_Image" onclick="this.style.display='none'; Codehighlighter1_623_1051_Open_Text.style.display='none'; Codehighlighter1_623_1051_Closed_Image.style.display='inline'; Codehighlighter1_623_1051_Closed_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_623_1051_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_623_1051_Closed_Text.style.display='none'; Codehighlighter1_623_1051_Open_Image.style.display='inline'; Codehighlighter1_623_1051_Open_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" alt="" />    </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> sessionDestroyed(HttpSessionEvent event) </span><span id="Codehighlighter1_623_1051_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.tkk7.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_623_1051_Open_Text"><span style="color: #000000">{<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" />        HttpSession session </span><span style="color: #000000">=</span><span style="color: #000000"> event.getSession();<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" />        </span><span style="color: #008000">//</span><span style="color: #008000"> 获取存储理员的session</span><span style="color: #008000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: #000000">        Object o </span><span style="color: #000000">=</span><span style="color: #000000"> session.getAttribute(Constants.JPXX_ADMIN_LOGIN_USERNAME_SESSION);<br /> <img id="Codehighlighter1_807_1045_Open_Image" onclick="this.style.display='none'; Codehighlighter1_807_1045_Open_Text.style.display='none'; Codehighlighter1_807_1045_Closed_Image.style.display='inline'; Codehighlighter1_807_1045_Closed_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_807_1045_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_807_1045_Closed_Text.style.display='none'; Codehighlighter1_807_1045_Open_Image.style.display='inline'; Codehighlighter1_807_1045_Open_Text.style.display='inline';" src="http://www.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" alt="" />        </span><span style="color: #0000ff">if</span><span style="color: #000000">(o</span><span style="color: #000000">!=</span><span style="color: #0000ff">null</span><span style="color: #000000">)</span><span id="Codehighlighter1_807_1045_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.tkk7.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_807_1045_Open_Text"><span style="color: #000000">{<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" />            String userName </span><span style="color: #000000">=</span><span style="color: #000000"> o.toString();<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" />            </span><span style="color: #008000">//</span><span style="color: #008000"> 登陆的用户都存储在Hashtable中,用户退出,从Hashtable中删除该用户</span><span style="color: #008000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: #000000">            Factory.getUsers().remove(userName);<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align="top" alt="" />            log.error(</span><span style="color: #000000">"</span><span style="color: #000000">删除用户Q?/span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> userName </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">       </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> java.util.Date());<br /> <img src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />        }</span></span><span style="color: #000000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />    }</span></span><span style="color: #000000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span><span style="color: #000000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span></div> 说明Q由于该cL自于目Q有很多操作都进行了装Q您E作修改卛_正常使用。注释掉LoggerQ声明一个静态变量private static Hashtable user = new Hashtable();在sessionCreated()Ҏ中将刚登陆的用户d到user中。您q需要写注销的页面(jsp或者servletQ,在页面里面调用session.invalidate(); 该方法会触发sessionDestroyed()事gQ?br /> <br />  <strong>   W二部:该监听器注册到web.xml<br /> </strong> <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"><img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #0000ff"><</span><span style="color: #800000">listener</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" />        </span><span style="color: #0000ff"><</span><span style="color: #800000">listener-class</span><span style="color: #0000ff">></span><span style="color: #000000">org.jpxx.sw.business.SessionListener</span><span style="color: #0000ff"></</span><span style="color: #800000">listener-class</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #0000ff"></</span><span style="color: #800000">listener</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> <img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span></div> 说明Q仅仅依靠session监听器不可能100%可靠Q有可能用户不正帔R?例如直接关闭览器、系l断늭{?。针对直接关闭浏览器有如下解x案:在被关闭的页面里面插入如下语句:<br /> <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"><img src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #0000ff"><</span><span style="color: #800000">body </span><span style="color: #ff0000">scroll</span><span style="color: #0000ff">="no"</span><span style="color: #ff0000"> onUnload</span><span style="color: #0000ff">="Jpxx.forceLogout();"</span><span style="color: #ff0000"> onbeforeunload</span><span style="color: #0000ff">="Jpxx.forceLogout();"</span><span style="color: #0000ff">></span></div> <p>Jpxx.forceLogout()Ҏ通过AJAX技术来强制注销用户Q即调用session.invalidate()Ҏ?</p> 转蝲:<a >http://www.jpxx.org/?tid=54</a> <img src ="http://www.tkk7.com/aoneany/aggbug/272005.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/aoneany/" target="_blank">沙漠中的?/a> 2009-05-21 16:02 <a href="http://www.tkk7.com/aoneany/articles/272005.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用JAVA的动态属性之反射原理实现一个简单AOP容器 - AOP的实现原理分?/title><link>http://www.tkk7.com/aoneany/articles/271053.html</link><dc:creator>沙漠中的?/dc:creator><author>沙漠中的?/author><pubDate>Sat, 16 May 2009 14:25:00 GMT</pubDate><guid>http://www.tkk7.com/aoneany/articles/271053.html</guid><wfw:comment>http://www.tkk7.com/aoneany/comments/271053.html</wfw:comment><comments>http://www.tkk7.com/aoneany/articles/271053.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/aoneany/comments/commentRss/271053.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/aoneany/services/trackbacks/271053.html</trackback:ping><description><![CDATA[<span>AOPQAspect Oriented ProgrammingQ意为面向切?局部的E序设计。它是面向对象的E序设计的一Ug伸?br /> <br /> 本文试图通过使用Java本n的动态属性,来实C个具有简单的AOP功能的容器?/span> <div><br /> 开始理解v来可能比较费Ԍ但我们可以通过一个通俗说明来这L解AOP的概念,是使用AOP可以不用修改原有的代码,而可以追加新的功能?br /> <br /> 比如Q我们用AOP实现了用L陆(判断ID与密码是否正)功能Q现在我们要求在用户登陆时用LOG记录用户登陆的情c一般的做法是直接修改已有的登陆逻辑代码Q但使用AOPQ可以不用修改原有的代码而完成此功能?br /> <br /> <br /> 本文试图通过使用Java本n的动态代理功能,来实C个具有简单的AOP功能的容器。从而帮助大家对AOP有个大概的认识?br /> <br /> <div id="ocm44y4" class="tf_edit_html_title1">Java动态代理功?/div> 首先Q我们简单介l一下Java动态代理功能。JAVA的动态代理功能主要通过java.lang.reflect.ProxycMjava.lang.reflect.InvocationHandler接口完成Q这里正是通过它们实现一个简单的AOP容器的。其实,像JBoss AOP{其他动态AOP框架也都是通过Proxy和InvocationHandler来实现的?br /> <br /> <br /> <ul> <li>Java从JDK1.3开始提供动态代理(Java ProxyQ功能? <li>所谓动态代理,即通过代理c:Proxy的代理,接口和实现类之间可以不直接发生联p,而可以在q行期(RuntimeQ实现动态关联? <li>AOPQAspect Oriented ProgrammingQ:面向切面~程Q其中的一U实现方法便是用Proxy来实现的? <li>Java Proxy只能代理接口Q不能代理类? <li>Java Proxy功能主要通过java.lang.reflect.ProxycMjava.lang.reflect.InvocationHandler接口实现? <li>java.lang.reflect.Proxy Q代理类Q?> ProxyInterfaceQ被代理的接口)   > InvocationHandlerQ关联类Q?gt; ClassQ可以在InvocationHandler中被调用Q。它们之间的关系可以用下面的程图来表示Q?</li> </ul> <br /> <br /> <div id="4q4uekc" class="tf_edit_html_box6">动态代理类QProxy 被代理的接口 InvocationHandler实现c?代理c?/div> <br />      实际上的调用关系可以用下面的程图来表示:<br /> <div id="wkca8e4" class="tf_edit_html_box6"> <div align="center"><br /> Proxy.newProxyInstance<br /> <br /> XxxxInterface xx = $ProxyN(N=0,1,2…)<br /> <br /> XxxxInterface.calledMethod<br /> <br /> $ProxyN.calledMethod<br /> <br /> InvocationHandler.invoke<br /> <br /> method.invoke(obj, args)<br /> <br /> obj.calledMethod</div> </div> <ol> <li>Proxy. newProxyInstance的参敎ͼ必须传送以?个参数给Proxy. newProxyInstanceҎQClassLoaderQClass[]QInvocationHandler。其中参?代理的接?ClassQ参?为实现InvocationHandler接口的实例? <li>可以通过Proxy. newProxyInstanceҎ得到被代理的接口的一个实例(instanceQ,该实例由newProxyInstanceҎ动态生成,q实C该接口? <li>当程序显C用接口的ҎӞ其时是调用该实例的方法,此方法又会调用与该实例相兌InvocationHandler的invokeҎ? <li>q样我们可以在InvocationHandler.invokeҎ里调用某些处理逻辑或真正的逻辑处理实现cR?</li> </ol> <br /> <br /> <br /> <div id="cgieqai" class="tf_edit_html_title1">用Java Proxy实现AOP容器</div> 下面我们使用Java Proxy来实C个简单的AOP容器?br /> 文g列表Q?br /> <table class="defTbl FCK__ShowTableBorders"> <tbody> <tr class="altEventRow"> <th>文g?/th> <th>说明</th> </tr> <tr class="eventRow"> <td>AopInvocationHandlerImpl.java</td> <td>该类实现了java.lang.reflect.InvocationHandler接口Q我们通过它记录LOG信息</td> </tr> <tr class="altEventRow"> <td>AopContainer.java</td> <td>单的AOP容器Q通过它把IDoBusiness与AopInvocationHandlerImpl兌h</td> </tr> <tr class="eventRow"> <td>IDoBusiness.java</td> <td>逻辑处理接口</td> </tr> <tr class="altEventRow"> <td>DoBusiness.java</td> <td>逻辑处理实现c?/td> </tr> <tr class="eventRow"> <td>TestAop.java</td> <td>试c?/td> </tr> </tbody> </table> <br /> 单的AOP容器Q?br /> AopContainer.java<br /> <div id="y4ag4ic" class="tf_edit_html_code"><span style="font-weight: bold; color: #7f0055">package</span> com.test.aop.framework;<br /> <br /> <span style="font-weight: bold; color: #7f0055">import</span> java.lang.reflect.Proxy;<br /> <br /> <span style="color: #3f5fbf">/**<br /> * A Simple AOP Container<br /> *<br /> */</span><br /> <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> AopContainer {<br />     <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> <T> T getBean(Class<T> interfaceClazz, <span style="font-weight: bold; color: #7f0055">final</span> T obj) {<br />          assert interfaceClazz.isInterface();<br />         <br />         <span style="font-weight: bold; color: #7f0055">return</span> (T) Proxy.newProxyInstance(interfaceClazz.getClassLoader(),<br />                 <span style="font-weight: bold; color: #7f0055">new</span> Class[] { interfaceClazz }, <span style="font-weight: bold; color: #7f0055">new</span> AopInvocationHandlerImpl(obj));<br />      }<br /> <br />     <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> Object getBean(<span style="font-weight: bold; color: #7f0055">final</span> Object obj) {<br />         <span style="font-weight: bold; color: #7f0055">return</span> Proxy.newProxyInstance(obj.getClass().getClassLoader(),<br />                  obj.getClass().getInterfaces(), <span style="font-weight: bold; color: #7f0055">new</span> AopInvocationHandlerImpl(obj));<br />      }<br /> }</div> W一个getBeanҎ通过2个参敎ͼW一个参Cؓ被代理的接口Q第二个参数代理的类实例Q?br /> W二个getBeanҎ只有一个参敎ͼ是cȝ实例。该cdd?个以上的接口。本文的例子q没有用到该方法,所以这里顺便介l一下它的用方法。比如有一个类HelloWorld实现了接口IHelloWorld1和IHelloWorld2Q那么可以通过<br /> <div id="q8484uc" class="tf_edit_html_code">HelloWorld helloWorld = <span style="font-weight: bold; color: #7f0055">new</span> HelloWorld();<br /> IHelloWorld1 helloWorld1 = (IHelloWorld1)AopContainer.getBean(helloWorld);<br /> <span style="color: #3f7f5f">//?br /> </span>IHelloWorld2 helloWorld2 = (IHelloWorld2)AopContainer.getBean(helloWorld);</div> 调用。当然很多时候都不会直接用new HelloWorld()生成HelloWorld实例Q这里ؓ了简便,q接用new生成HelloWorld实例了?br /> <br /> 实现InvocationHandler接口的中间类Q?br /> AopInvocationHandlerImpl.java<br /> <div id="g8a444u" class="tf_edit_html_code"><span style="font-weight: bold; color: #7f0055">package</span> com.test.aop.framework;<br /> <br /> <span style="font-weight: bold; color: #7f0055">import</span> java.lang.reflect.InvocationHandler;<br /> <span style="font-weight: bold; color: #7f0055">import</span> java.lang.reflect.Method;<br /> <br /> <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> AopInvocationHandlerImpl <span style="font-weight: bold; color: #7f0055">implements</span> InvocationHandler {<br />     <span style="font-weight: bold; color: #7f0055">private</span> Object bizPojo;<br />     <br />     <span style="font-weight: bold; color: #7f0055">public</span> AopInvocationHandlerImpl(Object bizPojo) {<br />          this.bizPojo = bizPojo;<br />      }<br /> <br />     <span style="color: #3f7f5f">/*<br />      * (non - Javadoc)<br />      * <br />      * <span style="font-weight: bold; color: #7f9fbf">@see</span> java.lang.reflect.InvocationHandler#invoke(java.lang.Object,<br />      *       java.lang.reflect.Method, java.lang.Object[])<br />      */</span><br />     <span style="font-weight: bold; color: #7f0055">public</span> Object invoke(Object proxy, Method method, Object[] args)<br />             <span style="font-weight: bold; color: #7f0055">throws</span> Throwable {<br />          Object o = null;<br />         <span style="font-weight: bold; color: #7f0055">try</span> {<br />              System.out.println("<span style="color: #2a00ff">Start:</span>" + method.getName());<br />              o = method.invoke(bizPojo, args);<br />              System.out.println("<span style="color: #2a00ff">End:</span>" + method.getName());<br />          } <span style="font-weight: bold; color: #7f0055">catch</span> (Exception e) {<br />              e.printStackTrace();<br />              System.out.println("<span style="color: #2a00ff">Exception Occured!</span>" + e.getMessage());<br />             <span style="color: #3f7f5f">// excetpion handling.<br /> </span>         }<br />         <span style="font-weight: bold; color: #7f0055">return</span> o;<br />      }<br /> }</div> AopInvocationHandlerImpl.invokeҎ的第一个参Cؓ代理c,在我们这个例子里为java.lang.reflect.Proxycȝ一个实例。第二个参数MethodQؓ被代理的接口的方法调用(实际上是自动生成代理cȝҎ调用Q,W三个方法ؓҎ调用的参数?br /> 我们通过在AopInvocationHandlerImpl.invokeҎ里的method.invoke(bizPojo, args)来调用bizPojocȝ与被代理接口的同名方法。这里,bizPojo必须实现了被代理的接口?br /> 在我们的例子里,我们在实际上被调用的业务逻辑Ҏ的前后输Z日志信息?br /> <br /> 实际上的逻辑处理cR该cdC被代理的接口QIDoBusiness?<br /> DoBusiness.java<br /> <div id="8q8k48o" class="tf_edit_html_code"><span style="font-weight: bold; color: #7f0055">package</span> com.test.aop.framework;<br /> <br /> <span style="color: #3f5fbf">/**<br /> * A business class<br /> *<br /> */</span><br /> <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> DoBusiness <span style="font-weight: bold; color: #7f0055">implements</span> IDoBusiness {<br />     <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> printNothing() {<br />          System.out.println("<span style="color: #2a00ff">Just Say Hello!</span>");<br />      }<br />     <br />     <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> throwException() {<br />         <span style="font-weight: bold; color: #7f0055">throw</span> <span style="font-weight: bold; color: #7f0055">new</span> RuntimeException("<span style="color: #2a00ff">throw Exception from DoBusiness.throwException()</span>");<br />      }<br /> <br /> }</div> <br /> 被代理的接口定义Q?br /> IDoBusiness.java<br /> <div id="884ogsc" class="tf_edit_html_code"><span style="font-weight: bold; color: #7f0055">package</span> com.test.aop.framework;<br /> <br /> <span style="color: #3f5fbf">/**<br /> * interface for business logic process<br /> *<br /> */</span><br /> <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">interface</span> IDoBusiness {<br />     <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> printNothing();<br />     <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> throwException();<br /> }</div> <br /> 试c:<br /> TestAop.java<br /> <div id="eu8ymu4" class="tf_edit_html_code"><span style="font-weight: bold; color: #7f0055">package</span> com.test.aop.framework;<br /> <br /> <br /> <span style="color: #3f5fbf">/**<br /> * Test AOP<br /> *<br /> */</span><br /> <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> TestAop {<br /> <br />     <span style="color: #3f5fbf">/**<br />      * <span style="font-weight: bold; color: #7f9fbf">@param</span> args<br />      */</span><br />     <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> <span style="font-weight: bold; color: #7f0055">void</span> main(String[] args) {<br />          DoBusiness doBusiness = <span style="font-weight: bold; color: #7f0055">new</span> DoBusiness();<br />          IDoBusiness idoBusiness = AopContainer.getBean(IDoBusiness.class, doBusiness);<br />          idoBusiness.printNothing();<br />         <br />          idoBusiness.throwException();<br />      }<br /> <br /> }</div> <br /> <strong>ȝQ?/strong><br /> 本文通过Java Proxy实现了一个简单地AOP容器。也单地展示了AOP的基本实现原理,实际上可以以此ؓ基础实现一个功能完善的AOP容器?/div> <p> </p> <p>转蝲:<a >http://hi.baidu.com/e9151/blog/item/9c8d772be0319d305243c130.html</a><br /> </p> <img src ="http://www.tkk7.com/aoneany/aggbug/271053.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/aoneany/" target="_blank">沙漠中的?/a> 2009-05-16 22:25 <a href="http://www.tkk7.com/aoneany/articles/271053.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于InvocationHandler接口http://www.tkk7.com/aoneany/articles/271019.html沙漠中的?/dc:creator>沙漠中的?/author>Sat, 16 May 2009 10:53:00 GMThttp://www.tkk7.com/aoneany/articles/271019.htmlhttp://www.tkk7.com/aoneany/comments/271019.htmlhttp://www.tkk7.com/aoneany/articles/271019.html#Feedback0http://www.tkk7.com/aoneany/comments/commentRss/271019.htmlhttp://www.tkk7.com/aoneany/services/trackbacks/271019.html       查看doc文档可以知道,在java.lang.reflect包中有一个叫Proxy的类。下面是doc文档对Proxycȝ说明Q?/p>

       "A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below. A proxy interface is such an interface that is implemented by a proxy class. A proxy instance is an instance of a proxy class. Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler."

        Proxycȝ设计用到代理模式的设计思想QProxycd象实C代理目标的所有接口,q代替目标对象进行实际的操作。但q种替代不是一U简单的替代Q这h有Q何意义,代理的目的是在目标对象方法的基础上作增强Q这U增强的本质通常是对目标对象的Ҏq行拦截。所以,Proxy应该包括一个方法拦截器Q来指示当拦截到Ҏ调用时作何种处理。InvocationHandler是拦截器的接口?/p>

      InvocationHandler接口也是在java.lang.reflec

     Object invoke(Object proxy, Method method, Object[] args)

     q个接口有三个参敎ͼ其中W二和第三个参数都比较好理解Q一个是被拦截的ҎQ一个是该方法的参数列表。关键是W一个参数。按照doc文档的解析,

      proxy - the proxy instance that the method was invoked on

      也就是说Qproxy应该是一个代理实例,但ؓ什么要传入q个参数呢?

      带着q个问题Q自q了个程序作了一点试验?/p>

///////////////////////////////////////

      public interface IAnimal {
           void info();
      }

////////////////////////////////////

    public class Dog implements IAnimal

    {

          public void info() {
             System.out.println("I am a dog!");
          }
    }

///////////////////////////////////////
import java.lang.reflect.*;

public class ProxyTest {
 public static void main(String[] args) throws InterruptedException {
  final IAnimal animal = new Dog();
  Object proxyObj =Proxy.newProxyInstance(
    animal.getClass().getClassLoader(),
    animal.getClass().getInterfaces(),
    new InvocationHandler()
    {
     public Object invoke(Object proxy, Method method, Object[] args)
     {
      try {
       System.out.println("被拦截的Ҏ:" + method.getName());
       return method.invoke(animal, args);
      }
      catch (IllegalArgumentException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       return null;
      } catch (IllegalAccessException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       return null;
      } catch (InvocationTargetException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       return null;
      }
     }
    });
  if(proxyObj instanceof IAnimal)
  {
   System.out.println("the proxyObj is an animal!");
  }
  else
  {
   System.out.println("the proxyObj isn't an animal!");
  }
  
  if(proxyObj instanceof Dog)
  {
   System.out.println("the proxyObj is a dog!");
  }
  else
  {
   System.out.println("the proxyObj isn't a dog!");
  }
  
  IAnimal animalProxy = (IAnimal)proxyObj;
  animalProxy.info();
  animalProxy.hashCode();
  System.out.println(animalProxy.getClass().getName().toString());
 }
}

E序执行的结果如下:

the proxyObj is an animal!
the proxyObj isn't a dog!
被拦截的Ҏ:info
I am a dog!
被拦截的Ҏ:hashCode
$Proxy0

从结果可以看Z下几点:

1. proxyObj 是一个实C目标对象接口的对象,而不同于目标对象。也是_q种代理机制是面向接口,而不是面向类的?/p>

2. infoҎ(在接口中)被成功拦截了QhashCodeҎ也成功被拦截了,但意外的是,getClassҎ(l承自Object cȝҎ)q没有被拦截Q!

3. 应用调试q可以看出Invocation接口中invokeҎ的传入的proxy参数实是代理对象实例proxyObj

ZgetClass()没有被拦截?proxy参数又有何用呢?

先不,做一个试验看看。既然这个proxy参数是代理实例对象Q它理所当然和proxyObj是一LQ可以调用info{方法。于是我们可以在invokeҎ中加上如下一条语句:

((IAnimal)proxy).info();

l果是:

the proxyObj is an animal!
the proxyObj isn't a dog!
被拦截的Ҏ:info
被拦截的Ҏ:info

.......

被拦截的Ҏ:info
被拦截的Ҏ:info

然后是栈溢?/p>

l果是很明显的,在invokeҎ中调用proxy中的Ҏ会再一ơ引发invokeҎQ这陷入了d@环,最l结果当然是栈溢出的?/p>

可以在invokeҎ中调用proxy.getClass(), E序可以正常q行。但如果调用hashCode()Ҏ同样会导致栈溢出?/p>

       通过上面的试验,可以得出一些初步结论,invoke 接口中的proxy参数不能用于调用所实现接口的方法。奇怪的是hashCode()和getClass()Ҏ都是从Object中承下来的ҎQؓ什么一个可以另一个不可以呢?带首疑问到doc文档看一下Object中这两个ҎQ发现getClass()是定义ؓfinal的,而hashCode()不是。难道是q个原因Q于是找C个非finalҎQ如equals试了一下,真的又会D栈溢出;扑֏一个finalҎ如wait(),试了一下,invoke又不拦截了。final N是关键之处Q?/p>

      q有一个问题就是proxy有什么用Q既然proxy可以调用getClass()ҎQ我们就可以得到proxy的Classc象Q从而可以获得关于proxy代理实例的所有类信息Q如Ҏ列表QAnnotation{,q就为我们提供的一个分析proxy的有力工P如通过分析Annotation分析Ҏ的声明式事务需求。我想传入proxy参数应该是这样一个用意吧?/p>

 

InvocationHandler的资?/strong>

属于包:java.lang.reflect

public interface InvocationHandler

InvocationHandler 是代理实例的调用处理E序 实现的接口?

每个代码实例都具有一个关联的调用处理E序。对代理实例调用ҎӞ对Ҏ调用q行~码q将其指zֈ它的调用处理E序?invoke Ҏ?/p>

从以下版本开始:
JDK1.3

invoke

Object proxy,
Object[] args)
throws Throwable - 从代理实例上的方法调用抛出的异常。该异常的类型必d以分配到在接口方法的 throws 子句中声明的M异常cd或未l检查的异常cd java.lang.RuntimeException ?java.lang.Error。如果此Ҏ抛出l过查的异常Q该异常不可分配到在接口Ҏ?throws 子句中声明的M异常cdQ代理实例的Ҏ调用抛出包含此Ҏ曾抛出的异常?

]]>从一个ConnectionPool的实现看design pattern的运?(source code for Java 1.1)http://www.tkk7.com/aoneany/articles/270995.html沙漠中的?/dc:creator>沙漠中的?/author>Sat, 16 May 2009 05:43:00 GMThttp://www.tkk7.com/aoneany/articles/270995.htmlhttp://www.tkk7.com/aoneany/comments/270995.htmlhttp://www.tkk7.com/aoneany/articles/270995.html#Feedback0http://www.tkk7.com/aoneany/comments/commentRss/270995.htmlhttp://www.tkk7.com/aoneany/services/trackbacks/270995.html阅读全文

]]>
做个好的Q事Ӟ侦听器笔?/title><link>http://www.tkk7.com/aoneany/articles/267761.html</link><dc:creator>沙漠中的?/dc:creator><author>沙漠中的?/author><pubDate>Mon, 27 Apr 2009 10:22:00 GMT</pubDate><guid>http://www.tkk7.com/aoneany/articles/267761.html</guid><wfw:comment>http://www.tkk7.com/aoneany/comments/267761.html</wfw:comment><comments>http://www.tkk7.com/aoneany/articles/267761.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/aoneany/comments/commentRss/267761.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/aoneany/services/trackbacks/267761.html</trackback:ping><description><![CDATA[<strong>事g监听的安全?/strong><br /> 如下代码Q在用Vectorq行不安全的q代Ӟ另外一个线E对Vector的对象进行添加或删除Ӟ会抛Z抛出 ConcurrentModificationException异常?br /> Vector<Listener> v;<br /> for (int i=0; i<v.size(); i++)<br />   v.get(i).eventHappened(event);<br /> java.util.concurrent 中的 CopyOnWriteArrayList c,能够帮助防止q个问题。它实现?ListQ而且是线E安全的Q但是它的P代器不会抛出 ConcurrentModificationExceptionQ遍历期间也不要求额外的锁定。这U特性组合是通过在每ơ列表修ҎQ在内部重新分配q拷贝列表内容而实现的Q这P遍历内容的线E不需要处理变?—?从它们的角度来说Q列表的内容在遍历期间保持不变。虽然这听v来可能没效率Q但是请CQ在多数观察者情况下Q每个组件只有少量侦听器Q遍历的数量q远过插入和删除的数量。所以更快的q代可以补偿较慢的变化过E,q提供更好的q发性,因ؓ多个U程可以同时q代列表?br /> <strong>事g监听初化风?/strong><br /> 事g监听器允?this"转义<br /> <strong>失效的监?/strong><br />   public void processFile(String filename) throws IOException {<br />     cancelButton.registerListener(this);<br />     // open file, read it, process it<br />     // might throw IOException<br />     cancelButton.unregisterListener(this);<br />   }<br /> 当读入文件时Q生异常,会造成取消注册事g不能被执行,取消登记的操作应当在 finally 块中q行Q这PprocessFile() Ҏ的所有出口都会执行它? <img src ="http://www.tkk7.com/aoneany/aggbug/267761.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/aoneany/" target="_blank">沙漠中的?/a> 2009-04-27 18:22 <a href="http://www.tkk7.com/aoneany/articles/267761.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://jj5c.com" target="_blank">޾ƷƵ</a>| <a href="http://www998xe.com" target="_blank">ֳִˬֳƵ </a>| <a href="http://pohezi.com" target="_blank">޾ƷѶ</a>| <a href="http://jxxitutu.com" target="_blank">ȫվ</a>| <a href="http://guilinsix.com" target="_blank">aƬѹۿ</a>| <a href="http://szicon.com" target="_blank">þ4438</a>| <a href="http://qzllw.com" target="_blank">Ļþһ</a>| <a href="http://a666999.com" target="_blank">hƬѹۿ</a>| <a href="http://catalna.com" target="_blank">֮4δɾ </a>| <a href="http://aa2176.com" target="_blank">պ߹ۿƵ</a>| <a href="http://fennenll.com" target="_blank">ϺɫϺҳa</a>| <a href="http://rr7733.com" target="_blank">Ʒ˸</a>| <a href="http://cdessc.com" target="_blank">պѵӰվ</a>| <a href="http://njjngs.com" target="_blank">ۺϾƷһ</a>| <a href="http://xuanboart.com" target="_blank">þþ޹</a>| <a href="http://xyzch.com" target="_blank">ѿAVëƬһ</a>| <a href="http://guoyit.com" target="_blank">þѹƷһ</a>| <a href="http://zjhydouyaji.com" target="_blank">ҹþ</a>| <a href="http://166sb.com" target="_blank">Ʒ޹AVƬý</a>| <a href="http://k8h9.com" target="_blank">һõþۺ</a>| <a href="http://cswsfz.com" target="_blank">91˿߹ۿ</a>| <a href="http://glhrsydc.com" target="_blank">aaëƬȫ</a>| <a href="http://dxj588.com" target="_blank">޾Ʒþþþϼ </a>| <a href="http://tlyyt.com" target="_blank">޹ҹɫ߹ۿ</a>| <a href="http://yy1514.com" target="_blank">޾Ʒxxx</a>| <a href="http://www77714.com" target="_blank">˵һ </a>| <a href="http://51caox.com" target="_blank">AVպAVվ </a>| <a href="http://woaisouluo.com" target="_blank">˳ɫ777777ͷ</a>| <a href="http://gift1town.com" target="_blank">ۺ뾫Ʒ </a>| <a href="http://jiajuco.com" target="_blank">av߹ۿ</a>| <a href="http://gdbobo.com" target="_blank">ѹۿ</a>| <a href="http://91sebo.com" target="_blank">鶹¹AVԭ</a>| <a href="http://jhc2go.com" target="_blank">ѹۿվ</a>| <a href="http://wlamyx.com" target="_blank">ŷۺһ</a>| <a href="http://mlhcd.com" target="_blank">˳777߲</a>| <a href="http://zgfxshop.com" target="_blank">þ99޾Ʒۿ</a>| <a href="http://dgyinhezy.com" target="_blank">þù˾Ʒ </a>| <a href="http://hnmeiankj.com" target="_blank">ŷպһ</a>| <a href="http://txa6.com" target="_blank">ѹƵ</a>| <a href="http://yytengsheng.com" target="_blank">һƵѹۿ</a>| <a href="http://38miao.com" target="_blank">պƷרӰ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>