先看代码

动态代理是 JDK 的动态代理,在 Java 的 JDK 中提供了一个 Proxy 用于创建动态代理的方法。

Proxy (Java Platform SE 8 ) 官方文档说明

Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.
To create a proxy for some interface Foo:

     InvocationHandler handler = new MyInvocationHandler(...);
     Class<?> proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), Foo.class);
     Foo f = (Foo) proxyClass.getConstructor(InvocationHandler.class).
                     newInstance(handler);

 
or more simply:
     Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
                                          new Class<?>[] { Foo.class },
                                          handler);

交易接口

/**
 * 动态代理 - 交易接口
 * @version 0.1
 * @author  BaiJing.biz
 */
public interface Trade {

    void sell();

}

代理商类

/**
 * 动态代理 - 代理商网点
 * @version 0.1
 * @author  BaiJing.biz
 */
public class AgentOutlets implements Trade {
    @Override
    public void sell() {
        System.out.println("Agent 代理商销售商品");
    }
}

代理工厂类

        —— 实现了动态代理,具体的代理执行体 invoke 

        —— 这是一个工厂类

        —— 代理类是动态生成的,结束后会自动销毁

package biz.baijing.proxy.dynamic;

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

/**
 * 动态代理 - 获取代理对象的工厂类
 * @version 0.1
 * @author  BaiJing.biz
 */
public class ProxyFactory {

    // 生成目标对象
    private AgentOutlets agentOutlets = new AgentOutlets();

    // 获取代理对象
    public Trade getProxyObject() {
        // 返回代理对象
        /*
               JDK 提供了 proxy 类,用于动态代理用于创建代理对象的方法
               newProxyInstance 有三个参数:
                    - ClassLoader loader : 类加载器,加载代理类,通过目标对象获取
                    - Class<?>[] interfaces :代理类实现的接口的自解码对象
                    - InvocationHandler h : 代理对象的调用处理方法
         */
        Trade proxyObject = (Trade) Proxy.newProxyInstance(
                agentOutlets.getClass().getClassLoader(),
                agentOutlets.getClass().getInterfaces(),
                new InvocationHandler() {
                    /*
                        Object proxy : 代理对象,同 proxyObject 是同一个对象
                        Method method : 对接口中的方法进行封装的方法(method)对象,这里就是 sell 方法
                        Object[] args : 待用方法的实际参数

                        return : 返回值就是方法的返回值,这里是 sell 的返回值
                     */
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("InvocationHandler 的 invoke 方法执行 ...");
                        // 分销店的业务逻辑,收取服务费
                        System.out.println("Distribution 加收收 Distribution Outlets 服务费 !!!(动态代理)");
                        // 执行目标对象的方法 (反射)
                        Object object = method.invoke(agentOutlets, args);
                        return object;
                    }
                }
        );
        return proxyObject;
    }
}

客户购买

public class CustomerRun {
    public static void main(String[] args) {

        // 创建代理工厂对象
        ProxyFactory factory = new ProxyFactory();

        // 获取代理对象
        Trade proxyObject = factory.getProxyObject();

        // 调用具体的方法
        proxyObject.sell();


    }
}

结果

CGLIB动态代理

设计模式(Design pattern)代理模式(Proxy Pattern)之动态代理(Dynamic Pattern)的 CGLIB 实现-CSDN博客

静态代理参考:

设计模式(Design pattern)代理模式(Proxy Pattern)之静态代理(Static Proxy)-CSDN博客

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐