Skip to content

代理模式在开源框架设计中的应用

1. Spring Framework中的代理模式

动态代理实现
java
// Spring AOP代理核心实现  
public class JdkDynamicAopProxy implements AopProxy, InvocationHandler {  
    private final Object target;  
    private final AdvisedSupport advised;  

    public Object getProxy(ClassLoader classLoader) {  
        // 创建动态代理  
        return Proxy.newProxyInstance(  
            classLoader,   
            target.getClass().getInterfaces(),   
            this  
        );  
    }  

    @Override  
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
        // 前置增强  
        MethodInterceptor methodInterceptor = advised.getMethodInterceptor();  

        // 执行方法拦截器  
        MethodInvocation invocation = new ReflectiveMethodInvocation(  
            proxy, target, method, args,   
            target.getClass(), methodInterceptor  
        );  

        return invocation.proceed();  
    }  
}  

// 事务代理示例  
public class TransactionProxy implements MethodInterceptor {  
    @Override  
    public Object invoke(MethodInvocation invocation) throws Throwable {  
        TransactionStatus status = null;  
        try {  
            // 开启事务  
            status = transactionManager.getTransaction();  

            // 执行目标方法  
            Object result = invocation.proceed();  

            // 提交事务  
            transactionManager.commit(status);  

            return result;  
        } catch (Exception e) {  
            // 回滚事务  
            transactionManager.rollback(status);  
            throw e;  
        }  
    }  
}

2. Dubbo中的代理模式

java
// Dubbo远程调用代理  
public class JavassistProxyFactory implements ProxyFactory {  
    @Override  
    public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {  
        return (T) Proxy.newProxyInstance(  
            invoker.getInterface().getClassLoader(),  
            interfaces,  
            new InvokerInvocationHandler(invoker)  
        );  
    }  

    // 远程调用代理处理器  
    static class InvokerInvocationHandler implements InvocationHandler {  
        private final Invoker<?> invoker;  

        @Override  
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
            // 远程调用拦截  
            RpcInvocation rpcInvocation = new RpcInvocation(  
                method,   
                invoker.getInterface(),   
                args  
            );  

            // 执行远程调用  
            return invoker.invoke(rpcInvocation).getValue();  
        }  
    }  
}

3. MyBatis中的代理模式

java
// MyBatis Mapper代理  
public class MapperProxy<T> implements InvocationHandler {  
    private final SqlSession sqlSession;  
    private final Class<T> mapperInterface;  

    @Override  
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
        // 处理Object方法  
        if (Object.class.equals(method.getDeclaringClass())) {  
            return method.invoke(this, args);  
        }  

        // 执行数据库操作  
        return sqlSession.invoke(method, args);  
    }  

    // 创建Mapper代理  
    public static <T> T newMapperProxy(  
        SqlSession sqlSession,   
        Class<T> mapperInterface  
    ) {  
        return (T) Proxy.newProxyInstance(  
            mapperInterface.getClassLoader(),  
            new Class[] { mapperInterface },  
            new MapperProxy<>(sqlSession, mapperInterface)  
        );  
    }  
}

代理模式的高级应用特性

  1. 功能增强
    • 性能监控
    • 安全控制
    • 日志记录
    • 事务管理
  2. 调用控制
    • 远程方法调用
    • 延迟加载
    • 缓存
    • 熔断降级
  3. 设计优势
    • 解耦系统组件
    • 不修改原有代码
    • 动态扩展功能
    • 提高代码可维护性

实现技术

  1. 静态代理
    • 编译期生成代理类
    • 需要手动编写代理类
    • 扩展性较差
  2. 动态代理
    • JDK Proxy
    • Cglib
    • Javassist
    • 运行时动态生成
  3. 字节码增强
    • AspectJ
    • Byte Buddy
    • 直接修改字节码

性能与最佳实践

  1. 选择合适的代理技术
  2. 避免过度使用代理
  3. 关注性能开销
  4. 合理设计代理层次
  5. 使用缓存优化代理创建

适用场景

  1. 远程调用
  2. 权限控制
  3. 日志记录
  4. 性能监控
  5. 事务管理
  6. 缓存
  7. 懒加载

潜在挑战

  1. 性能开销
  2. 调试复杂性
  3. 代理链管理
  4. 接口兼容性

代理模式是开源框架中最重要的设计模式之一,它通过在目标对象和调用者之间增加一层代理,实现了横切关注点的解耦和功能增强。这种模式不仅提供了灵活的扩展机制,还为系统架构提供了强大的动态能力。

更新: 2025-01-25 21:00:00
原文: https://www.yuque.com/tulingzhouyu/db22bv/ev4c8ea9iu54pxyo