JDK动态代理模拟实现

代理工厂类:就是构造代理对象的

package com.wpc.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @author 一介草莽子
 * version 1.0
 */
@SuppressWarnings({"all"})
public class ProxyFactory {
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    public Object getProxy(){
/**
 * ClassLoader loader:指定生成动态代理生成的代理类的类加载器(ProxyFactory)
 * Class[] interfaces:获得目标对象实现的所有接口的class的数组(Calculator接口)
 * 因为代理类要和目标类要实现一样的接口功能
 * InvocationHandler h :设置代理对象实现目标方法的过程,即代理类如何重写接口中的方法
 */
        ClassLoader classLoader = this.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();

        InvocationHandler h= new InvocationHandler(){
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                /**
                 * proxy:代理对象
                 * Method:表示要执行的方法
                 * args:表示要指向的方法里面的参数
                 */
                Object result = null;
                try {
                    System.out.println("日志 方法: "+method.getName()+" 参数: "+ Arrays.toString(args));
                    //调用目标对象的方法
                    result = method.invoke(target, args);
                    System.out.println("日志 方法: "+method.getName()+" 结果: "+ result);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("日志 方法: "+method.getName()+" 异常: "+ e);

                }
                finally {
                    System.out.println("日志 方法: "+method.getName()+" 最后: "+result);

                }

                return result;
            }
        };

        return  Proxy.newProxyInstance( classLoader, interfaces, h);
    

}

Proxy.newProxyInstance( classLoader, interfaces, h);方法参数解析:

 ClassLoader loader:指定生成动态代理生成的代理类的类加载器(ProxyFactory)
 Class[] interfaces:获得目标对象实现的所有接口的class的数组(Calculator接口)
 因为代理类要和目标类要实现一样的接口功能
 InvocationHandler h :设置代理对象实现目标方法的过程,即代理类如何重写接口中的方法

InvocationHandler()的invoke方法参数解析:
 proxy:代理对象
 Method:表示要执行的方法
 args:表示要指向的方法里面的参数

 

接口实现类

package com.wpc.proxy;

/**
 * @author 一介草莽子
 * version 1.0
 */
@SuppressWarnings({"all"})
public class CalculatorImpl implements Calculator {
    public int add(int i, int j) {
        int result=i+ j;

        return result;
    }

    public int sub(int i, int j) {
        int result=i- j;

        return result;
    }

    public int mul(int i, int j) {
        int result=i* j;

        return result;
    }

    public int div(int i, int j) {
        int result=i/ j;

        return result;
    }
}

接口:

package com.wpc.proxy;

/**
 * @author 一介草莽子
 * version 1.0
 */
@SuppressWarnings({"all"})
public interface Calculator {
    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值