动态代理原理梳理

本文通过一个示例展示了Java动态代理的实现过程,包括接口定义、接口实现和调用处理器的创建。代码中使用了`Proxy.newProxyInstance()`生成代理类,并在调用处理器中添加了额外的行为,如在方法调用前后打印日志。动态代理在运行时生成的类为`$Proxy0`,实现了`IApp`接口并包含了相应的方法实现。
摘要由CSDN通过智能技术生成

接口类定义

package com.example.demo.test;

public interface IApp  {
    void doSomething(String str);
}

接口类实现

package com.example.demo.test;

public class AppServiceImpl implements IApp {
    @Override
    public void doSomething(String str) {
        System.out.println(str);
    }
}

调用处理器

package com.example.demo.test;

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

public class AppProxyHandler implements InvocationHandler {

    private AppServiceImpl appService;

    public AppServiceImpl getAppService() {
        return appService;
    }

    public AppProxyHandler setAppService(AppServiceImpl appService) {
        this.appService = appService;
        return this;
    }

    public AppProxyHandler(AppServiceImpl appService) {
        this.appService = appService;
    }

    public AppProxyHandler() {

    }


    @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(args);
        System.out.println("before");
        Object invoke = method.invoke(this.appService, args);
        System.out.println("after");
        return invoke;
    }
}

代码运行

package com.example.demo.test;

import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
        //JDK1.8及以前的版本,生成保存代理类文件,生成目录在同级目录的根目录
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        //JDK1.8以后的版本
        //System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");
        AppServiceImpl appService = new AppServiceImpl();
        IApp proxyInstance = (IApp) Proxy.newProxyInstance(appService.getClass().getClassLoader(), appService.getClass().getInterfaces(), new AppProxyHandler(appService));
        IApp proxyInstance2 = (IApp) Proxy.newProxyInstance(appService.getClass().getClassLoader(), appService.getClass().getInterfaces(), new AppProxyHandler2(appService));
        proxyInstance.doSomething("hello proxy");
        //两个实例的class都是$Proxy0;只是处理方式(invocationHander不一样)
        System.out.println(proxyInstance.getClass());
        System.out.println(proxyInstance2.getClass());
    }
}

生成的动态代理类

package com.sun.proxy;

import com.example.demo.test.IApp;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public final class $Proxy0 extends Proxy implements IApp {
    private static Method m1;
    private static Method m2;
    private static Method m3;
    private static Method m0;

    public $Proxy0(InvocationHandler var1) throws  {
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {
            return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final String toString() throws  {
        try {
            return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final void doSomething(String var1) throws  {
        try {
            super.h.invoke(this, m3, new Object[]{var1});
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final int hashCode() throws  {
        try {
            return (Integer)super.h.invoke(this, m0, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }
    //这里定义好各个方法
    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m3 = Class.forName("com.example.demo.test.IApp").getMethod("doSomething", Class.forName("java.lang.String"));
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

参考文档:

填坑之动态代理生成的代理类文件在哪?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值