<pre name="code" class="java">
<div class="blog_title" style="margin-bottom: 15px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 18px;"><h3 style="font-size: 16px; line-height: 1.5em; margin: 0px 0px 0.5em; padding: 10px 0px 0px;"><span style="font-size: 14px; line-height: 1.8em;">Java中Proxy,Dynamic Proxy以及Reflection是什么关系呢?谈这个问题之前,我们要先了解三者究竟是什么东西。</span></h3></div><div id="blog_content" class="blog_content" style="font-size: 14px; line-height: 1.8em; font-family: Helvetica, Tahoma, Arial, sans-serif;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"> 一、Proxy。面向对象中Proxy是比较常用的,作为GOF 23个模式之一,Proxy在Design Patterns一书中有着详细的描述。Proxy的使用范围很广,下面以一个简单的访问控制为例。在这种情况下,外部使用者要想使用某个对象,必须通过该对象的代理进行。在这里,Proxy充当着“网关”的角色,这样,在外部触及到内部对象之前,我们可以做很多有意义的工作,如身份认证等。下面是一个简单的例子:</p><div class="code_title">java 代码</div><div class="dp-highlighter" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; background-color: transparent; width: 679px; overflow: auto; margin-left: 9px; padding: 1px; word-break: break-all; word-wrap: break-word;"><div class="bar"><ol class="dp-j" style="font-size: 1em; line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); list-style-position: initial; list-style-image: initial; color: rgb(43, 145, 175);"><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> ProxyTest { <span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//劳工使用方 </span> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span style="color: black;"> main(String[] args) { </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> Job job = </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span style="color: black;"> Proxy(</span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span style="color: black;"> Worker()); </span><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//劳工使用方找到代理,让它进行code的工作 </span><span style="color: black;"> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> job.code(); </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">interface</span> Job{ </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span style="color: black;"> code(); </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> Proxy <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">implements</span> Job{ <span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//代理声称可以code </span> </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">private</span><span style="color: black;"> Worker wrk; </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> Proxy(Worker wrk){ </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span style="color: black;">.wrk = wrk; </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span style="color: black;"> code(){ </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//代理把工作交给worker之前,可以进行使用者是否合法 </span><span style="color: black;"> </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> System.out.println(</span><span class="string" style="color: blue;">"Check the permission"</span><span style="color: black;">); </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> wrk.code(); </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//worker做完以后,代理还可以做更多的工作 </span><span style="color: black;"> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> System.out.println(</span><span class="string" style="color: blue;">"Get the saraly"</span><span style="color: black;">); </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//Worker实际做code的工作,但是使用者不能直接找到他, </span> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//而要通过代理 </span> </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> Worker <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">implements</span> Job{ </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span style="color: black;"> code() { </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> System.out.println(</span><span class="string" style="color: blue;">"Worker code actually"</span><span style="color: black;">); </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li></ol></div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;">
二、Dynamic Proxy。上面的Proxy,也可以叫做静态代理,它的特征是代理和实际作业的对象的类都是在编译期间就已经定下来的。与此相对的是动态代理,其代理是在程序运行期间动态产生的。Java对动态代理提供了相应的API,但只能接口方法的动态代理。动态代理一个典型应用就是实现数据库的连接池。由于使用JDBC的程序员习惯在使用完数据库的Connection对象后调用该对象的close方法关闭连接,这样连接池就失去作用了。这个问题我们可以使用动态代理技术来解决,当程序员调用Connection的close方法时,我们将其拦截下来,将其归还到连接池中,而不是直接关闭物理连接。下面是动态代理的一个实例。</p><div class="code_title">java 代码</div><div class="dp-highlighter" style="background-color: transparent; width: 658.625px; overflow: auto; margin-left: 9px; padding: 1px; word-break: break-all; word-wrap: break-word;"><div class="bar"><ol class="dp-j" style="font-size: 1em; line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); list-style-position: initial; list-style-image: initial; color: rgb(43, 145, 175);"><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> DynamicProxy { </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span style="color: black;"> main(String[] args) </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span style="color: black;"> Exception { </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> Job job = JobFactory.getJob(); </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> job.code(); </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">interface</span> Job{ </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span style="color: black;"> code(); </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> Worker <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">implements</span> Job{ </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span style="color: black;"> code() { </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> System.out.println(</span><span class="string" style="color: blue;">"worker code"</span><span style="color: black;">); </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span style="color: black;"> play(){ </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> System.out.println(</span><span class="string" style="color: blue;">"worker play"</span><span style="color: black;">); </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> Handler <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">implements</span> InvocationHandler{ </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">private</span><span style="color: black;"> Worker worker; </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> Handler(Worker worker){ </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span style="color: black;">.worker = worker; </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> Object invoke(Object obj, Method mtd, Object[] objs) </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span style="color: black;"> Throwable { </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> Object res = </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span style="color: black;">; </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> System.out.println(</span><span class="string" style="color: blue;">"check permission"</span><span style="color: black;">); </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> res = mtd.invoke(worker, objs); </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> System.out.println(</span><span class="string" style="color: blue;">"get the saraly"</span><span style="color: black;">); </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span style="color: black;"> res; </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> JobFactory { </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span style="color: black;"> Job getJob() { </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> Worker worker = </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span style="color: black;"> Worker(); </span><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//这是代理,可以代理接口指定的工作 </span><span style="color: black;"> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span style="color: black;"> (!(worker </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">instanceof</span><span style="color: black;"> Job ))</span><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//判断是否有资格当这些工作的代理 </span><span style="color: black;"> </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span style="color: black;">; </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">/* construct a invocation handler with the impl instance */</span><span style="color: black;"> </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> InvocationHandler handler = </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span style="color: black;"> Handler(worker); </span><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">//这是实际工作的人 </span><span style="color: black;"> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">/* get the class info, and the class loader used by this factory */</span><span style="color: black;"> </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> Class[] interfaces = </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span style="color: black;"> Class[] { Job.</span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span><span style="color: black;">}; </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> ClassLoader loader = JobFactory.</span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span><span style="color: black;">.getClassLoader(); </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> Job proxy = (Job) Proxy.newProxyInstance(loader, interfaces, handler); </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span style="color: black;"> proxy; </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> } </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">} </span></li><li class="alt" style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"> </span></li></ol></div> 动态代理与普通的代理相比较,最大的好处是接口中声明的所有方法都被转移到一个集中的方法中处理(invoke),这样,在接口方法数量比较多的时候,我们可以进行灵活处理,而不需要像静态代理那样每一个方法进行中转。在JDBC连接池的例子中,如果我们使用普通的代理模式进行,那么我们要对Connection中的每一个方法进行单独处理,那么是比较繁琐的。如果,我们用动态代理的方式,实现起来就比较优雅。
三、Reflection。Java中通过反射技术,我们可以在运行过程中得到相应类的各种方法和属性,并且可以进行修改和调用。动态代理技术就是基于反射实现的。当然,反射技术用处还很多,譬如JavaBean技术。
现在是比较清楚了,Proxy和Dynamic Proxy都是一种模式,两者实现的功能相当,但是方式不同;Reflection是实现后者的技术基础。</div></div></div>