﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-少年阿宾-随笔分类-Java动态代理</title><link>http://www.blogjava.net/stevenjohn/category/52559.html</link><description>那些青春的岁月</description><language>zh-cn</language><lastBuildDate>Thu, 06 Sep 2012 16:23:33 GMT</lastBuildDate><pubDate>Thu, 06 Sep 2012 16:23:33 GMT</pubDate><ttl>60</ttl><item><title>Java动态代理实现</title><link>http://www.blogjava.net/stevenjohn/archive/2012/09/07/387212.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Thu, 06 Sep 2012 16:22:00 GMT</pubDate><guid>http://www.blogjava.net/stevenjohn/archive/2012/09/07/387212.html</guid><wfw:comment>http://www.blogjava.net/stevenjohn/comments/387212.html</wfw:comment><comments>http://www.blogjava.net/stevenjohn/archive/2012/09/07/387212.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stevenjohn/comments/commentRss/387212.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stevenjohn/services/trackbacks/387212.html</trackback:ping><description><![CDATA[<p align="center" style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; text-align: center; "><strong><span style="font-size: 14pt; ">Java</span></strong><strong><span style="font-size: 14pt; font-family: 宋体; ">动态代理实现</span></strong><strong></strong></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p align="center" style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; text-align: center; "><strong><span style="font-size: 12pt; ">Kongxx</span></strong></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: 宋体; ">在目前的</span>Java<span style="font-family: 宋体; ">开发包中包含了对动态代理的支持，但是其实现只支持对接口的的实现。</span></p><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: 宋体; ">其实现主要通过是</span>java.lang.reflect.Proxy<span style="font-family: 宋体; ">类和</span>java.lang.reflect.InvocationHandler<span style="font-family: 宋体; ">接口。</span></p><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; ">Proxy<span style="font-family: 宋体; ">类主要用来获取动态代理对象，</span>InvocationHandler<span style="font-family: 宋体; ">接口用来约束调用者实现，如下，</span>HelloWorld<span style="font-family: 宋体; ">接口定义的业务方法，</span>HelloWorldImpl<span style="font-family: 宋体; ">是</span>HelloWorld<span style="font-family: 宋体; ">接口的实现，</span>HelloWorldHandler<span style="font-family: 宋体; ">是</span>InvocationHandler<span style="font-family: 宋体; ">接口实现。代码如下：</span></p><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: 宋体; ">业务接口</span>:</p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><table cellspacing="0" cellpadding="0" border="1" style="margin: 0px auto; border: medium none; border-collapse: collapse; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #e6e6e6; background-position: 0% 50%; "><tbody><tr><td valign="top" width="568" style="font-size: 12px; border: 1pt solid windowtext; border-collapse: collapse; padding: 0cm 5.4pt; word-break: normal !important; width: 426.1pt; background-color: transparent; "><p style=" font-size: 13px; line-height: 19px; ">public interface HelloWorld {</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void sayHelloWorld() ;</p><p style=" font-size: 13px; line-height: 19px; ">}</p></td></tr></tbody></table><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: 宋体; ">业务接口实现：</span></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><table cellspacing="0" cellpadding="0" border="1" style="margin: 0px auto; border: medium none; border-collapse: collapse; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #e6e6e6; background-position: 0% 50%; "><tbody><tr><td valign="top" width="568" style="font-size: 12px; border: 1pt solid windowtext; border-collapse: collapse; padding: 0cm 5.4pt; word-break: normal !important; width: 426.1pt; background-color: transparent; "><p style=" font-size: 13px; line-height: 19px; ">public class HelloWorldImpl implements HelloWorld {</p><p>&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void sayHelloWorld() {</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Hello World!");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style=" font-size: 13px; line-height: 19px; ">}</p></td></tr></tbody></table><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; ">InvocationHandler<span style="font-family: 宋体; ">实现，需要在接口方法调用前后加入一部份处理工作，这里仅仅在方法调用前后向后台输出两句字符串，其代码如下：</span></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><table cellspacing="0" cellpadding="0" border="1" style="margin: 0px auto; border: medium none; border-collapse: collapse; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #e6e6e6; background-position: 0% 50%; "><tbody><tr><td valign="top" width="568" style="font-size: 12px; border: 1pt solid windowtext; border-collapse: collapse; padding: 0cm 5.4pt; word-break: normal !important; width: 426.1pt; background-color: transparent; "><p style=" font-size: 13px; line-height: 19px; ">import java.lang.reflect.InvocationHandler;</p><p style=" font-size: 13px; line-height: 19px; ">import java.lang.reflect.Method;</p><p style=" font-size: 13px; line-height: 19px; "></p><p>public class HelloWorldHandler implements InvocationHandler {</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<span style="font-family: 宋体; ">要代理的原始对象</span></p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private Object objOriginal;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/**</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<span style="font-family: 宋体; ">构造函数。</span></p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* @param obj&nbsp;<span style="font-family: 宋体; ">要代理的原始对象。</span></p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public HelloWorldHandler(Object obj) {</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.objOriginal = obj ;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style=" font-size: 13px; line-height: 19px; "></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public Object invoke(Object proxy, Method method, Object[] args)</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws Throwable {</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object result ;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<span style="font-family: 宋体; ">方法调用之前</span></p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doBefore();</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<span style="font-family: 宋体; ">调用原始对象的方法</span></p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = method.invoke(this.objOriginal ,args);</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<span style="font-family: 宋体; ">方法调用之后</span></p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doAfter();</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return result ;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private void doBefore() {</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("before method invoke!");</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private void doAfter() {</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("after method invoke!");</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style=" font-size: 13px; line-height: 19px; ">}</p></td></tr></tbody></table><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: 宋体; ">测试代码：</span></p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><table cellspacing="0" cellpadding="0" border="1" style="margin: 0px auto; border: medium none; border-collapse: collapse; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #e6e6e6; background-position: 0% 50%; "><tbody><tr><td valign="top" width="568" style="font-size: 12px; border: 1pt solid windowtext; border-collapse: collapse; padding: 0cm 5.4pt; word-break: normal !important; width: 426.1pt; background-color: transparent; "><p style=" font-size: 13px; line-height: 19px; ">import java.lang.reflect.InvocationHandler;</p><p style=" font-size: 13px; line-height: 19px; ">import java.lang.reflect.Proxy;</p><p style=" font-size: 13px; line-height: 19px; "></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">public class Test {</p><p style=" font-size: 13px; line-height: 19px; "></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {</p><p style=" font-size: 13px; line-height: 19px; "></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HelloWorld hw = new HelloWorldImpl();</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InvocationHandler handler = new HelloWorldHandler(hw);</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HelloWorld proxy = (HelloWorld) Proxy.newProxyInstance(</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hw.getClass().getClassLoader(),</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hw.getClass().getInterfaces(),</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler);</p><p>&nbsp;</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy.sayHelloWorld();</p><p style=" font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style=" font-size: 13px; line-height: 19px; ">}</p></td></tr></tbody></table><p style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt; font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: Wingdings; ">&#216;<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="font-family: 宋体; ">首先获取一个业务接口的实现对象；</span></p><p style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt; font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: Wingdings; ">&#216;<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="font-family: 宋体; ">获取一个</span>InvocationHandler<span style="font-family: 宋体; ">实现，此处是</span>HelloWorldHandler<span style="font-family: 宋体; ">对象；</span></p><p style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt; font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: Wingdings; ">&#216;<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="font-family: 宋体; ">创建动态代理对象；</span></p><p style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt; font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: Wingdings; ">&#216;<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="font-family: 宋体; ">通过动态代理对象调用</span>sayHelloWorld()<span style="font-family: 宋体; ">方法，此时会在原始对象</span>HelloWorldImpl. sayHelloWorld()<span style="font-family: 宋体; ">方法前后输出两句字符串。</span></p><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: 宋体; ">运行测试类输出如下</span>:</p><p style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "></p><table cellspacing="0" cellpadding="0" border="1" style="margin: 0px auto; border: medium none; border-collapse: collapse; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #e6e6e6; background-position: 0% 50%; "><tbody><tr><td valign="top" width="568" style="font-size: 12px; border: 1pt solid windowtext; border-collapse: collapse; padding: 0cm 5.4pt; word-break: normal !important; width: 426.1pt; background-color: transparent; "><p style=" font-size: 13px; line-height: 19px; ">before method invoke!</p><p style=" font-size: 13px; line-height: 19px; ">Hello World!</p><p style=" font-size: 13px; line-height: 19px; ">after method invoke!</p></td></tr></tbody></table><p style=" font-size: 13px; line-height: 19px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff; "><span style="font-family: 宋体; ">此处</span>Test<span style="font-family: 宋体; ">类中的方法调用代码比较多，在我们的实际应用中可以通过配置文件来来简化客户端的调用实现。另外也可以通过动态代理来实现简单的</span>AOP<span style="font-family: 宋体; ">。<br /><br /></span><a href="http://www.cnblogs.com/kongxx/archive/2005/08/08/209749.html">http://www.cnblogs.com/kongxx/archive/2005/08/08/209749.html</a>&nbsp;<span style="font-family: 宋体; "><br /></span></p><img src ="http://www.blogjava.net/stevenjohn/aggbug/387212.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stevenjohn/" target="_blank">abin</a> 2012-09-07 00:22 <a href="http://www.blogjava.net/stevenjohn/archive/2012/09/07/387212.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>