<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <span style="line-height:1.5; color:rgb(165,42,42)">1.什么是动态代理?</span></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <span style="line-height:1.5; color:rgb(165,42,42)">答:动态代理可以提供对另一个对象的访问,同时隐藏实际对象的具体事实。代理一般会实现它所表示的实际对象的接口。代理可以访问实际对象,但是延迟实现实际对象的部分功能,实际对象实现系统的实际功能,代理对象对客户隐藏了实际对象。客户不知道它是与代理打交道还是与实际对象打交道。<br> 2.为什么使用动态代理?</span></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <span style="line-height:1.5; color:rgb(165,42,42)">答:因为动态代理可以对请求进行任何处理</span></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <span style="line-height:1.5; color:rgb(165,42,42)">3.使用它有哪些好处?</span></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <span style="line-height:1.5; color:rgb(165,42,42)">答:因为动态代理可以对请求进行任何处理</span><span style="line-height:1.5; color:rgb(165,42,42)"><br> 4.哪些地方需要动态代理?</span></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <span style="line-height:1.5; color:rgb(165,42,42)">答:不允许直接访问某些类;对访问要做特殊处理等</span></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <span style="line-height:1.5; color:rgb(165,42,42)"> </span></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> 目前Java开发包中包含了对动态代理的支持,但是其实现只支持对<span style="line-height:1.5; color:rgb(255,0,0)"><strong>接口</strong></span>的的实现。 其实现主要通过java.lang.reflect.Proxy类和java.lang.reflect.InvocationHandler接口。 </p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> Proxy类主要用来获取动态代理对象,InvocationHandler接口用来约束调用者实现</p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> 以下为模拟案例,通过动态代理实现在方法调用前后向控制台输出两句字符串</p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> 目录结构</p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-0c444004cd009049e6aaf981ceadac05.png" style="border:0px"></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> <br/></p>
<p style="line-height:19.5px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> 定义一个HelloWorld接口</p>
<div class="cnblogs_code" style="border:1px solid rgb(204,204,204); padding:5px; overflow:auto; margin:5px 0px; font-family:'Courier New'!important; background-color:rgb(245,245,245)">
<div class="cnblogs_code_toolbar" style="margin-top:5px">
<span class="cnblogs_code_copy" style="padding-right:5px; line-height:1.5!important"><a style="color:rgb(26,139,200); border:none!important" target="_blank" title="复制代码"><img alt="复制代码" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-69c5a8ac3fa60e0848d784a6dd461da6.gif" style="border:none!important"></a></span>
</div>
<pre style="margin-top:0px; margin-bottom:0px; white-space:pre-wrap; word-wrap:break-word; font-family:'Courier New'!important">
<div style="background-color:rgb(245,245,245)">
<span style="color:rgb(0,128,128); line-height:1.5!important"> 1</span>
<span style="color:rgb(0,0,255); line-height:1.5!important">package</span>
<span style="line-height:1.5!important"> com.ljq.test;
</span>
<span style="color:rgb(0,128,128); line-height:1.5!important"> 2</span>
<span style="line-height:1.5!important">
</span>
<span style="color:rgb(0,128,128); line-height:1.5!important"> 3</span>
<span style="line-height:1.5!important"> </span>
<span style="color:rgb(0,128,0); line-height:1.5!important">/**</span>
<span style="color:rgb(0,128,0); line-height:1.5!important">
</span>
<span style="color:rgb(0,128,128); line-height:1.5!important"> 4</span>
<span style="color:rgb(0,128, |
|