<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### JDK動態代理 核心思想:通過實現被代理類的所有接口,生成一個字節碼文件后構造一個代理對象,通過持有反射構造被代理類的一個實例,再通過invoke反射調用被代理類實例的方法,來實現代理。 缺點:JDK動態代理的對象必須實現一個或多個接口 ![](https://img.kancloud.cn/5a/c3/5ac38b63215466f15a7aa8ad8d5cdf9c_770x546.png) ### 流程圖 ![](https://img.kancloud.cn/d1/fd/d1fd7d1db902e355833f858deb2dfe3f_1464x798.png) 知識點 * JDK實現動態代理需要實現類通過接口定義業務方法 * JDK生成的代理類以”$Proxy”為開頭進行命名 * JDK代理生成的代理類的Method在static靜態代碼塊中進行初始化; * public接口生成的代理類package為”com.sun.proxy”; * JDK動態代理需要實現InvocationHandler接口; ### 代碼 ``` import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class DynamicProxy { public static void main(String[] args) { Student stu = new Student(); CusInvocationHandler handler = new CusInvocationHandler(stu); Play instance = (Play) Proxy.newProxyInstance(stu.getClass().getClassLoader(), stu.getClass().getInterfaces(), handler); instance.play(); } } class CusInvocationHandler implements InvocationHandler { private Object target; public CusInvocationHandler(Object object) { this.target = object; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.err.println("代理方法處理"); return method.invoke(target, args); } } interface Play { void play(); } class Student implements Play { @Override public void play() { System.err.println("student ====>"); } } ``` 生成的代理類示例: ``` public final class $Proxy11 extends Proxy implements UserService { // 構造方法,參數就是剛才傳過來的MyInvocationHandler類的實例 public $Proxy11(InvocationHandler invocationhandler) { super(invocationhandler); } public final boolean equals(Object obj) { // 省略 } /** * 被代理的方法 */ public final void add() { try { // 實際上就是調用MyInvocationHandler的public Object invoke(Object proxy, Method method, Object[] args)方法,第二個問題就解決了 super.h.invoke(this, m3, null); return; } catch(Error _ex) { } catch(Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } public final int hashCode() { // 省略 } public final String toString() { // 省略 } private static Method m1; private static Method m3; private static Method m0; private static Method m2; // 在靜態代碼塊中獲取了4個方法:Object中的equals方法、UserService中的add方法、Object中的hashCode方法、Object中toString方法 static { try { m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") }); m3 = Class.forName("dynamic.proxy.UserService").getMethod("add", new Class[0]); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]); m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]); } catch(NoSuchMethodException nosuchmethodexception) { throw new NoSuchMethodError(nosuchmethodexception.getMessage()); } catch(ClassNotFoundException classnotfoundexception) { throw new NoClassDefFoundError(classnotfoundexception.getMessage()); } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看