JDK動態代理(Proxy)的兩種實現方式


  JDK自帶的Proxy動態代理兩種實現方式

  

  前提條件:JDK Proxy必須實現對象接口

  so,創建一個接口文件,一個實現接口對象,一個動態代理文件

  接口文件:TargetInterface.java

      

package proxy;

public interface TargetInterface {
    public String method1();
    public void method2();
    public int method3(int x);
}

 

  實現接口對象的Class文件:Target.java

  

package proxy;

public class Target implements TargetInterface{

    @Override
    public String method1() {
        // TODO Auto-generated method stub
        System.out.println("method1 running...");
        return "aaa";
    }

    @Override
    public void method2() {
        // TODO Auto-generated method stub
        System.out.println("method2 running...");
    }

    @Override
    public int method3(int x) {
        // TODO Auto-generated method stub
        return x;
    }

}

 

  動態代理的兩種實現方式

    1.ProxyTest.java

    

package proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.junit.Test;

public class ProxyTest {
    @Test
    public void test1() {
        TargetInterface newProxyInstance = (TargetInterface) Proxy.newProxyInstance(
                Target.class.getClassLoader(), 
                new Class[] {TargetInterface.class},
                new InvocationHandler() {
                    //invoke 代表的是執行代理對象的方法
                    @Override
                    //method:代表目標對象的方法字節碼對象
                    //args:代表目標對象的響應的方法的參數
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        // TODO Auto-generated method stub
                        Object invoke = method.invoke(new Target(), args);
                        return invoke;
                    }
                });
        
        String method1 = newProxyInstance.method1();
        newProxyInstance.method2();
        int method3 = newProxyInstance.method3(100);
        System.out.println(method1);
        System.out.println(method3);
    }
}

 

  2.ProxyTest2.java

    

package proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.junit.Test;

public class ProxyTest2 {
    
    public static void main(String[] args) {
        Target target = new Target();
        TargetInterface newProxyInstance = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(), 
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //反射知識點
                        Object invoke = method.invoke(target, args);
                        return invoke;
                    }
                });
        String method1 = newProxyInstance.method1();
        newProxyInstance.method2();
        int method3 = newProxyInstance.method3(100);
        System.out.println(method1);
        System.out.println(method3);
    }

}

  運行結果都如下所示:

  

總結下重要的就是:

1.熟練Proxy.newProxyInstance的使用

2.JDK的Proxy一定要實現接口。

 

  

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM