Java——动态代理

package com.ruqi.xiushifu.method;

// 作为被代理的对象,需要实现接口类
public class Star implements Skill{
    @Override
    public void run() {
        System.out.println("对象本身的逻辑,run");
    }

    @Override
    public void jump() {
        System.out.println("对象本身的逻辑,jump");

    }
}

=======================================
package com.ruqi.xiushifu.method;

public interface Skill {
    void run();
    void jump();
}
=======================================
实现代理逻辑
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class GetAgentProxy {

    public static Skill gtAgentProxy(Star obj){
        /**
         * (ClassLoader loader, 类加载器,固定写法
         * Class<?>[] interfaces, 添加被代理的接口实现
         * InvocationHandler h) , 代理的实际操作
         */

        return (Skill) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                new InvocationHandler() {
                    // method 表示代理时调用的方法,通过反射进行调用,args表示方法添加的参数
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("代理前置操作");
                        Object rs = method.invoke(obj, args);
                        System.out.println("代理后置操作");

                        return rs;
                    }
                });
    }
}

====================================
调用代理逻辑

public class Test {
    public static void main(String[] args) {
        Star s = new Star();
        Skill sk = GetAgentProxy.gtAgentProxy(s); //获取代理对象
        sk.jump();
        sk.run();
    }
}







 通用的代理方法,传入目标对象即可

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

public class GetProxy {


    public static Object getProxyObject(Object taget){
        ClassLoader classLoader = taget.getClass().getClassLoader();
        Class<?>[] interfaces = taget.getClass().getInterfaces();
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("代理前一步");
                Object obj = method.invoke(taget, args);
                System.out.println("代理后一步");
                return obj;
            }
        };
        return Proxy.newProxyInstance(classLoader,interfaces,invocationHandler);
    }
}

=================================
public class Testcase {
    public static void main(String[] args) {
        InterfaceTool proxyObject = (InterfaceTool)GetProxy.getProxyObject(new InterfacetoolImpl());
        proxyObject.add(1, 2);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑*杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值