動態代理類的實現和解析

 靜態代理類java

  由程序員建立或由特定工具自動生成源代碼,再對其編譯。在程序運行前,代理類的.class文件就已經存在了程序員

動態代理類:
   與靜態代理類對照的是動態代理類,動態代理類的字節碼在程序運行時由Java反射機制動態生成,無需程序員手工編寫它的源代碼。動態代理類不只簡化了編程工做,並且提升了軟件系統的可擴展性,由於Java 反射機制能夠生成任意類型的動態代理類。java.lang.reflect 包中的Proxy類和InvocationHandler 接口提供了生成動態代理類的能力。編程

話很少說看代碼。。ide

 

package proxyBase;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.util.Arrays;

import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class MyProxy {
    static Class<?>[] parameterTypes = { InvocationHandler.class };
    protected InvocationHandler h;

    protected MyProxy(InvocationHandler h) {
        this.h = h;
    }

    public static Object newInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) {
        // :產生一個代理類的字節碼對象
        Class<?> c1 = null;
        try {
            // :經過getClass0方法能夠獲得一個代理類的字節碼對象
            c1 = getClass0(loader, interfaces);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        Constructor<?> constructor = null;
        try {
            /**
             * 調用子類$Proxy0(InvocationHandler h)構造函數,因爲繼承了MyProxy類,
             * 因此又會繼續調用父類的MyProxy(InvocationHandler h)構造函數給 h初始化一個值;
             */
            constructor = c1.getDeclaredConstructor(InvocationHandler.class);
            /**
             * 返回一個生成的代理類對象,並將InvocationHandler 的實現類對象傳入進去。從而達到給h賦值的目的
             * 
             */
            return constructor.newInstance(h);
        } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
                | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    // :用來生成一個代理類對象
    private static Class<?> getClass0(ClassLoader loader, Class<?>[] interfaces) throws MalformedURLException {
        Class<?> forName = null;
        try {
            GennerateClass.generate(interfaces[0]);
            // :動態編譯
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null, null, null);
            Iterable units = fileMgr.getJavaFileObjects(System.getProperty("user.dir") + "/src/"
                    + interfaces[0].getPackage().getName() + "/" + interfaces[0].getSimpleName() + "$proxy0.java");
            // :"-d", System.getProperty("user.dir")+"/bin/" 用來指定java文件編譯後存放的地方
            Iterable<String> options = Arrays.asList("-d", System.getProperty("user.dir") + "/bin/");
            CompilationTask t = compiler.getTask(null, fileMgr, null, options, null, units);
            t.call();
            try {
                fileMgr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // :獲得代碼自動生成代理類的實例對象
            forName = Class.forName(interfaces[0].getName() + "$proxy0");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return forName;
    }
}

 這個GennerateClass類是用來生成代理類的Java文件的,是經過字符串拼接而成,僅供參考.函數

  1 package proxyBase;
  2 
  3 import java.io.BufferedWriter;
  4 import java.io.File;
  5 import java.io.FileWriter;
  6 import java.io.IOException;
  7 import java.lang.reflect.Method;
  8 
  9 public class GennerateClass {
 10     public static void generate(Class<?> clazz) {
 11         String methodStr = "";  //:方法字符串的拼接
 12         String classStr = "";    //:類名的拼接
 13         String packageStr = "";    //:導入包名的拼接
 14         String classParamStr = ""; //
 15         String staticCodeStr = "static {\ntry {";//:靜態代碼塊的拼接
 16         String member_var = "";  //:成員變量的拼接
 17         String package1 = clazz.getPackage().getName();
 18         String simpleName = clazz.getSimpleName(); //:得到簡單類名
 19         String className = clazz.getName();    //:得到權限定類名
 20         //:構造函數的拼接
 21         String counstructStr = "public " + simpleName + "$proxy0(InvocationHandler h) {\r\n" + "        super(h);\r"
 22                 + "    }\n";
 23         // :導包
 24         packageStr = "package " + package1 + ";\n"
 25                 + "import proxyBase.MyProxy;\r import java.lang.reflect.InvocationHandler;\n"
 26                 + "import java.lang.reflect.Method;\n";
 27         // :構建類名
 28         classStr += "public class " + simpleName + "$proxy0 extends MyProxy implements " + simpleName + "{\n" + "";
 29         // :構建代理類的方法
 30         Method[] methods = clazz.getMethods();
 31         int i = 0;
 32         for (Method method : methods) {
 33             String paramStr = "";//:參數變量拼接
 34             int paramCount = 0; //:參數個數計數用來生成參數變量
 35             i += 1;
 36             member_var += "private static Method m" + i + ";\n";//成員變量的拼接
 37             String tempStr = "";  //:參數列表
 38             String methodName = method.getName();// 方法名
 39             Class<?>[] parameterTypes = method.getParameterTypes();// 參數列表的class類型
 40             Class<?> returnType = method.getReturnType();// 返回值類型
 41             methodStr += "public final " + returnType.getName() + " " + methodName + "(";
 42             // :參數列表名字符串
 43             for (Class<?> type : parameterTypes) {
 44                 paramCount += 1;
 45                 tempStr += "," + type.getName() + " param" + paramCount;
 46                 paramStr += ",param" + paramCount;
 47                 classParamStr += "," + type.getName() + ".class";
 48             }
 49             //
 50             if (!paramStr.isEmpty()) {
 51                 paramStr = paramStr.substring(1);
 52             }
 53             if (!tempStr.isEmpty()) {
 54                 tempStr = tempStr.substring(1);
 55             }
 56             if (classParamStr.isEmpty()) {
 57                 classParamStr = "null";
 58             } else {
 59                 classParamStr = classParamStr.substring(1);
 60             }
 61             //:判斷返回值是否時void,是則不須要return
 62             //方法的拼接
 63             if (returnType.getName().equals("void")) {
 64                 methodStr = methodStr + tempStr + ")\n{\n" + "\ttry{\n\tthis.h.invoke(this,m" + i + ",new Object[]{"
 65                         + paramStr + "});" + "} catch (Throwable e) {\r\n" + "            e.printStackTrace();\r\n"
 66                         + "        }\n}\n";
 67             } else {
 68                 methodStr = methodStr + tempStr + ")\n{\nObject result=null;\n\ttry{\n\tresult=this.h.invoke(this,m" + i
 69                         + ",new Object[]{" + paramStr + "});" + "} catch (Throwable e) {\r\n"
 70                         + "            e.printStackTrace();\r\n" + "}\nreturn (" + returnType.getName() + ")result;}\n";
 71             }
 72             // :構建靜態代碼塊
 73             if (!classParamStr.equals("null")) {
 74                 staticCodeStr += "m" + i + " = Class.forName(\"" + className + "\").getMethod(\"" + methodName
 75                         + "\",new Class<?>[]{" + classParamStr + "});";
 76             } else {
 77                 staticCodeStr += "m" + i + " = Class.forName(\"" + className + "\").getMethod(\"" + methodName + "\");";
 78             }
 79             classParamStr = "";
 80         }
 81         //靜態代碼塊的拼接
 82         staticCodeStr += "} catch (NoSuchMethodException e) {\r\n" + "            e.printStackTrace();\r\n"
 83                 + "        } catch (SecurityException e) {\r\n" + "            e.printStackTrace();\r\n"
 84                 + "        } catch (ClassNotFoundException e) {\r\n" + "            e.printStackTrace();\r\n" + "        }}";
 85         //總和成Java文件的內容
 86         packageStr = packageStr + classStr + member_var + counstructStr + methodStr + staticCodeStr + "\n}";
 87         //經過流寫入成文件
 88         FileWriter fout = null;
 89         try {
 90             fout = new FileWriter(new File("src/" + package1 + "/" + simpleName + "$proxy0.java"));
 91         } catch (IOException e) {
 92 
 93             e.printStackTrace();
 94         }
 95         BufferedWriter out = new BufferedWriter(fout);
 96         try {
 97             out.write(packageStr);
 98         } catch (IOException e) {
 99             e.printStackTrace();
100         }
101         try {
102             out.close();
103             fout.close();
104         } catch (IOException e) {
105             e.printStackTrace();
106         }
107     }
108 }
View Code

 

/***
 *這個類是用來制定你的代理對象,在調用方法時須要進行哪些前置處理和後置處理
 *
*/
1
package proxyBase; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5 6 public class Hadler<T> implements InvocationHandler { 7 private T target; 8 //經過new Hadler將被代理的那個對象傳入, 9 public Hadler(T target) { 10 super(); 11 this.target = target; 12 } 13 private void before() { 14 System.out.println("先吃飯"); 15 } 16 private void after() { 17 System.out.println("再睡覺"); 18 } 19 //這裏根據本身的邏輯來制定相應的invoke方法,視狀況而定,此處的寫法只是來簡單測試代理對象。 20 @Override 21 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 22 before(); 23 Object invoke = method.invoke(target,args); 24 after(); 25 return invoke; 26 } 27 }

這裏時生成後的代理類文件Person$proxy0.java工具

package proxyBase;

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

public class Hadler<T> implements InvocationHandler {
    private T target;
    //經過new Hadler將被代理的那個對象傳入,
    public Hadler(T target) {
        super();
        this.target = target;
    }
    private void before() {
        System.out.println("先吃飯");
    }
    private void after() {
        System.out.println("再睡覺");
    }
    //這裏根據本身的邏輯來制定相應的invoke方法,視狀況而定,此處的寫法只是來簡單測試代理對象。
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(target,args);
        after();
        return invoke;
    }
}

接下來是寫一個接口,jdk中的動態代理是針對接口代理的,而cglib是針對類進行代理的,這個接口將會被代理,分別寫了四個方法來測試,無參,一參,兩參,和一個有返回值測試

的方法,都是用來測試GennerateClass類生成的是否正確,(目前只測試了這幾種,若是大神發現有誤,還望聯繫斧正)this

1 package proxy;
2 
3 
4 public interface Person {
5     void study2();
6     void study2(int a);
7     void study3(int b ,String a);
8     int returnInt(int a);
9 }

接下來寫一個Person的實現類,而後經過代理類來代理這個實現類對象,進行一些前置處理和後置處理。spa

 1 package proxy;
 2 
 3 public class Student implements Person {
 4     public void study2() {
 5         System.out.println("正在考試中");
 6     }
 7 
 8     @Override
 9     public void study2(int a) {
10         System.out.println(a);
11     }
12 
13     @Override
14     public void study3(int b, String a) {
15         System.out.println(a+b);
16     }
17 
18     @Override
19     public int returnInt(int a) {
20         
21         return a;
22     }
23 }

最後就是測試和使用所寫的代理類了.net

第一步,建立一個Person類型的對象student,

第二步,建立一個InvocationHandler的實現類對象,並將student傳入進去,這個student會將生成實現類中的成員變量target進行賦值初始化。

第三步,調用MyProxy中的newInstance方法來得到代理類對象(注意:newInstance 中的參數要是實現類的類類型來得到他的實現Interface接口的類類型,即Person,生成代理類的Java文件就是依據該接口生成的)

第四步, 測試生成的代理類對象,結果以下,經過原生student和代理類對象stu調用相同方法進行對比

 1 public static void main(String[] args) {
 2         Person student = new Student();
 3         
 4         InvocationHandler h = new Hadler<Person>(student);
 5         Person stu = (Person) MyProxy.newInstance(Person.class.getClassLoader(), Student.class.getInterfaces(), h);
 6         stu.study2();
 7         System.out.println("-------");
 8         student.study2();
 9         stu.study2(4);
10         stu.study3(4, "A");
11         int a = stu.returnInt(2015);
12         System.out.println(a);
13     }

運行結果:

先吃飯
正在考試中
再睡覺
-------
正在考試中
先吃飯
4
再睡覺
先吃飯
A4
再睡覺
先吃飯
再睡覺
2015
相關文章
相關標籤/搜索