高仿JDK动态代理 底层源码实现

动态代理实现思路

实现功能:通过Proxy.newProxyInstance返回代理对象

1、创建一个处理业务逻辑的接口,我们也和JDK一样,都使用InvocationHandler作为接口名,然后接口里面一个invoke方法,jdk呢是有三个参数,我们简化了一下就只要两个参数即可。
2、声明一段java代码字符串(动态产生代理类)
3、使用IO创建代理类的java文件,将java代码字符串写入java文件
4、编译代理类,通过URLClassLoader把代理类加载到内存中
5、使用Constructor(构造器)来创建代理类的实例
6、return 代理类

一、创建一个处理业务逻辑的接口(InvocationHandler)
package com.handler;
import java.lang.reflect.Method;
 
public interface InvocationHandler {
    public Object invoke(Method method,Object args[]);
}

二、创建一个Proxy里面只有一个静态方法newProxyInstance

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import com.handler.InvocationHandler;
public class Proxy {
    /**
     * 动态生成一个动态代理类
     * @param interfaceClass 实现的接口
     * @param handler 处理的业务逻辑
     * @return
     */                                          
    public static Object newProxyInstance(Class interfaceClass,InvocationHandler handler){
        String interfaceName=interfaceClass.getName();
        String interfaceSimpleName=interfaceClass.getSimpleName();
        String enter="\n";
        String tab="\t";
 
        //生成代码字符串1
        String proxyStr1="import "+interfaceName+";\n"
                + "import com.handler.InvocationHandler;\n"
                + "import java.lang.reflect.Method;"+
                enter+"public class Proxy$ implements "+interfaceSimpleName+"{"+
                enter+tab+"InvocationHandler handler;"+
                enter+tab+"public Proxy$("+handler.getClass().getName()+" handler){"+
                enter+tab+tab+"this.handler=handler;"+
                enter+tab+"}";
        
        //生成代码字符串2
        String proxyStr2="";
        //获得所接口的所有方法
        Method me[]=interfaceClass.getMethods();
        for (Method method : me) {
            
            String paramStr="";  //形参字符串
            String paramName=""; //参数名字字符串
            String paramClass="";//参数类型字符串
            int i=1;
            
            //得到方法所有参数
            Class paramTypeClass[]=method.getParameterTypes();
            for (Class class1 : paramTypeClass) {           //遍历所有的参数
                paramStr+=class1.getSimpleName()+" args"+i+","; //动态得到形参
                paramName+="args"+i+",";            //动态创建各个参数名字
                paramClass+=class1.getSimpleName()+".class,";   //动态创建获得“参数类型”的“类对象”字符串
                 i++;
            }
            
            //判断该方法是否有参数,有参数的话 length一定会大于0的
            if(paramStr.length()>0){     
                //截取掉最后一个"," 
                paramStr=paramStr.substring(0,paramStr.length()-1);  
                paramName=paramName.substring(0,paramName.length()-1);
                paramClass=","+paramClass.substring(0,paramClass.length()-1);       
            }
            
            //得到返回值类型
            String returnType=method.getReturnType().getSimpleName();
            proxyStr2+=enter+tab+"public "+returnType+" "+method.getName()+"("+paramStr+") {"+
                    
            enter+tab+tab+"try{";
            //判断是否有返回值
            if(returnType.equals("void")){ 
                            //生成获得Method的字符串                            
                proxyStr2+=enter+tab+tab+"Method md ="+interfaceSimpleName+".class.getMethod(\""+method.getName()+"\""+paramClass+");";
                //调用传入handler的invoke方法,实现代理功能       
                proxyStr2+=enter+tab+tab+"handler.invoke(md,new Object[]{"+paramName+"});";
                proxyStr2+=enter+tab+tab+"} catch (Exception e) {"+
                        enter+tab+tab+"e.printStackTrace();"+
                        enter+tab+tab+"}";
                proxyStr2+=enter+tab+"}";
            }else{
                proxyStr2+=enter+tab+tab+"Method md ="+interfaceSimpleName+".class.getMethod(\""+method.getName()+"\""+paramClass+");"; 
                proxyStr2+=enter+tab+tab+"return ("+returnType+") handler.invoke(md,new Object[]{"+paramName+"});";
                proxyStr2+=enter+tab+tab+"} catch (Exception e) {"+
                        enter+tab+tab+"e.printStackTrace();"+
                        enter+tab+tab+"}";
                        
                        //判断返回类型是否为基本数据类型
                        if( returnType.equals("int")||
                          returnType.equals("short")||
                           returnType.equals("long")||
                           returnType.equals("char")||
                          returnType.equals("float")||
                         returnType.equals("double")||
                           returnType.equals("byte")){
                            proxyStr2+=enter+tab+tab+"return 0;";
                        }else if(returnType.equals("boolean")){
                            proxyStr2+=enter+tab+tab+"return false;";
                        }else{
                            proxyStr2+=enter+tab+tab+"return null;";
                        }
                        
                proxyStr2+=enter+tab+"}";
            }
            
        }
     proxyStr2+=enter+tab+"}";
 
     //创建proxy$.java文件到项目外的一个目录下
     File file=new File("C:\\Users\\Administrator\\AppData\\Local\\Temp\\Proxy$.java");
     try {
        file.createNewFile();
        FileWriter fw=new FileWriter(file);
        fw.write(proxyStr1+proxyStr2);//把生成的代码字符串写入到创建的java文件中
        fw.flush();
        
        //使用javaCompiler(java编译器)编译刚才创建的java类
        JavaCompiler  compiler = ToolProvider.getSystemJavaCompiler();
        //文件管理者
        StandardJavaFileManager fileMange = compiler.getStandardFileManager(null, null, null);
        //获取文件
        Iterable it=fileMange.getJavaFileObjects(file.getPath());
        //编译任务
        CompilationTask t = compiler.getTask(null, fileMange, null, null, null, it);
        //进行编译
        t.call();
        
        //使用URL得到该java类所在的路径,并放入URL数组中
        URL url = new URL("file:/C:\\Users\\Administrator\\AppData\\Local\\Temp\\");
        URL[] urls = new URL[]{url};
        
        //通过URLClassLoader(URL类加载器)加载urls里面的类到内存中
        URLClassLoader  urlClassLoader = new URLClassLoader(urls);
        //通过类全名得到类加载器中的代理类的类对象  (这里填写类全名,因为该类没有包名,所以这里的全名就是该java文件名)
        Class clazzProxy =urlClassLoader.loadClass("Proxy$");
        
        //因为我们产生的java类不是在我们的项目中,所以,我们不能使用 Class.forName()来得到该类的实例,
        //那么我们可以通过“Constructor(构造器)”来得到一个类的实例
        @SuppressWarnings("unchecked")
        //从代理类中得到一个构造方法,通过构造方法来实例该类
        Constructor constructor= clazzProxy.getConstructor(handler.getClass());
        Object object =constructor.newInstance(handler);
        //返回代理类
        return object;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     
        
        return null;
    }
 
}

三、好啦, 那么到这里我们的jdk动态代理已经模仿完成啦, 我们来测试一下吧

1、我们模仿的动态代理和jdk动态代理,使用时大同小异的哈。我们新建一个日志的代理类吧,让他实现我们的业务逻辑处理接口,并重写invoke方法

package com.dao;
import java.lang.reflect.Method;
import com.handler.InvocationHandler;
 
public class StudentDaoImplTimeProxy implements InvocationHandler{
    Object obj;
    public StudentDaoImplTimeProxy(Object obj) {
        this.obj=obj;
    }
        @Override
        public Object invoke(Method method, Object[] args) {    
            Object ob=null;
            try {
                 System.out.println("===========开始记录日志===========");         
                ob=method.invoke(obj,args);
                 System.out.println("===========结束记录===========");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return ob;
        }
}

2、我们代理的目标类

package com.dao;
public class StudentDaoImpl implements StudentDao2 {
    @Override
    public void query(int i, String str) {
        // TODO Auto-generated method stub
        System.out.println("########查询数据库#######");
    }
    @Override
    public void add(int i) {
        // TODO Auto-generated method stub
        System.out.println("########添加数据#######"); 
    }
}

3、实现的接口

package com.dao;
 
public interface StudentDao {
    public int query(int i);
    public void add(); 
}
package com.test;
 
import java.lang.reflect.Method;
 
import com.dao.StudentDao;
import com.dao.StudentDao2;
import com.dao.StudentDaoImpl;
import com.dao.StudentDaoImpl1;
import com.dao.StudentDaoImplLogProxy;
import com.dao.StudentDaoImplTimeProxy;
import com.handler.InvocationHandler;
import com.proxy.Proxy;
 
public class Test {
public static void main(String[] args) {
    StudentDao sd=new StudentDaoImpl1();
    //custom仿jdk动态代理
    InvocationHandler h1=new StudentDaoImplTimeProxy(sd);
    StudentDao dao1=(StudentDao)Proxy.newProxyInstance(StudentDao.class,h1);    
    System.out.println(dao1.query(1)); 
}
image.png

image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容

  • 一、基本概念 1.什么是代理? 在阐述JDK动态代理之前,我们很有必要先来弄明白代理的概念。代理这个词本身并不是计...
    小李弹花阅读 16,390评论 2 40
  • https://blog.csdn.net/luanlouis/article/details/24589193 ...
    小陈阿飞阅读 804评论 1 1
  • 本篇将从源码层面分析,JDK代理的具体实现方式。摘录源码版本:JDK 1.8 概述 我们知道,在Spring AO...
    _Zy阅读 1,069评论 0 3
  • title: Jdk动态代理原理解析 tags:代理 categories:笔记 date: 2017-06-14...
    行径行阅读 19,075评论 3 36
  • 不只是一个微笑 不只是一句问候 不只是一件礼物 是感动感动了微笑 感动了问候 感动了礼物 是真心
    曹广潼树根草阅读 50评论 0 1