<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            java語言

            JAVA的動態代理分析

            時間:2025-04-28 05:04:21 java語言 我要投稿
            • 相關推薦

            JAVA的動態代理分析

              引導語:在使用動態代理類時,我們必須實現InvocationHandler接口,以下是百分網小編分享給大家的JAVA動態代理,歡迎閱讀了解!

              1,被代理類 的接口 Proxied

              Java代碼

              package Test1;

              // 被代理類 需實現的 接口

              public interface Proxied {

              void doSomething();

              void doSomethingElse(String str);

              }

              2,一個 Proxied接口 的實現類(被代理類)

              Java代碼

              package Test1;

              public class ConcreteProxied implements Proxied {

              @Override

              public void doSomething() {

              try {

              Thread.sleep(100);

              } catch (InterruptedException e) {

              System.err.println("Error : InterruptedException");

              }

              System.out.println(this.getClass().getSimpleName()

              + " >> doSomething .");

              }

              @Override

              public void doSomethingElse(String str) {

              try {

              Thread.sleep(150);

              } catch (InterruptedException e) {

              System.err.println("Error : InterruptedException");

              }

              System.out.println(this.getClass().getSimpleName()

              + " >> doSomethingElse , argument = " + str + ".");

              }

              }

              3,TimingInvocationHandler 類,實現了 InvocationHandler 接口

              Java代碼

              package Test1;

              import java.lang.reflect.InvocationHandler;

              import java.lang.reflect.Method;

              public class TimeingInvocationHandler implements InvocationHandler{

              //被代理的對象

              private Object proxied;

              public TimeingInvocationHandler(Object proxied){

              this.proxied = proxied;

              }

              // 參數 proxy 表示代理類的對象

              // 參數 method 表示被代理類 和 代理類 都實現的接口 的方法對象

              // 參數 args 表示方法 method 的參數數組

              @Override

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

              throws Throwable {

              System.out.println(method.getDeclaringClass().getName());

              long currentTimeMillis = System.currentTimeMillis();

              Object ret = method.invoke(proxied, args);

              System.out.println(this.getClass().getSimpleName()+" >> wastes time : "

              +(System.currentTimeMillis() - currentTimeMillis)+"ms");

              return ret;

              }

              }

              4,測試類 Test

              Java代碼

              package Test1;

              import java.lang.reflect.Proxy;

              public class TestProxy {

              public static void main(String[] args) {

              Proxied proxied = new ConcreteProxied();

              proxied.doSomething();

              proxied.doSomethingElse("only a String");

              // 生成一個代理實例,這個代理實現了 Proxied 接口

              // 對這個代理(proxy)的方法的調用 會 重定向到 TimeingInvocationHandler 的 invoke 方法

              Proxied proxy = (Proxied) Proxy.newProxyInstance(Proxied.class

              .getClassLoader(), // 類加載器

              new Class[] { Proxied.class }, // 代理要實現的接口

              new TimeingInvocationHandler(proxied) // 調用處理器

              );

              proxy.doSomething();

              proxy.doSomethingElse("only a String");

              }

              }

              運行Test類,輸出如下:

              ConcreteProxied >> doSomething .

              ConcreteProxied >> doSomethingElse , argument = only a String.

              Test1.Proxied

              ConcreteProxied >> doSomething .

              TimeingInvocationHandler >> wastes time : 110ms

              Test1.Proxied

              ConcreteProxied >> doSomethingElse , argument = only a String.

              TimeingInvocationHandler >> wastes time : 156ms

            【JAVA的動態代理分析】相關文章:

            java使用動態代理來實現AOP05-29

            幾分鐘理解Java中的動態代理07-25

            java利用反射實現動態代理實現代碼10-08

            JAVA的動態考試系統的設計10-07

            java動態方法調度實例10-01

            關于Java動態實現的方法08-23

            Java中如何實現顯示動態的時間09-28

            JNI是怎樣java調用c動態庫07-09

            Java中靜態綁定和動態綁定的區別08-14

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看