设计模式-代理模式

2024-03-12 1452阅读

温馨提示:这篇文章已超过435天没有更新,请注意相关的内容是否还可用!

代理模式UML类图:

设计模式-代理模式

代理模式,旨在代理某个对象的职责,并且可以在职责代理过程中,新增其他辅助方法,代理模式也叫作委托模式。

Subject抽象主题角色:被代理角色的抽象角色

RealSubject具体主题角色:被代理角色

Proxy代理角色:对被代理角色实现方法增强

一、JDK动态代理

JDK动态代理是基于反射原理实现,可以查看JDK提供的InvocationHandler接口和Proxy类

1、被代理对象抽象接口

package com.example.demo.proxy;
public interface SubjectInterface {
	void coreMethod();
}

2、具体被代理对象

package com.example.demo.proxy;
public class Subject implements SubjectInterface {
	@Override
	public void coreMethod() {
		System.out.println("具体角色");
	}
}

3、增强被代理对象

package com.example.demo.proxy;
public class Advice {
	public void before(){
		System.out.println("前置处理");
	}
	public void after(){
		System.out.println("后置处理");
	}
}

4、JDK动态代理测试

package com.example.demo.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JdkTest {
	public static void main(String[] args) {
		// 被代理对象
		Subject subject = new Subject();
		// 增强对象
		Advice advice = new Advice();
		InvocationHandler handler = new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				advice.before();
				Object invoke = method.invoke(subject, args);
				advice.after();
				return invoke;
			}
		};
		SubjectInterface proxy = (SubjectInterface) Proxy.newProxyInstance(
				subject.getClass().getClassLoader(), // 传入ClassLoader
				subject.getClass().getInterfaces(), // 传入要实现的接口
				handler); // 传入处理调用方法的InvocationHandler
		// 执行方法
		proxy.coreMethod();
	}
}

在运行期动态创建一个interface实例的方法如下:

  1. 定义一个InvocationHandler实例,它负责实现接口的方法调用
  2. 通过Proxy.newProxyInstance()创建interface实例,它需要3个参数:
    1. 使用的ClassLoader,通常就是接口类的ClassLoader
    2. 需要实现的接口数组,至少需要传入一个接口进去
    3. 用来处理接口方法调用的InvocationHandler实例
  3. 将返回的Object强制转型为接口

动态代理实际上是JVM在运行期动态创建class字节码并加载的过程

二、CGLIB动态代理

CGLIB是一个强大的高性能的代码生成包,通过CGLIB的依赖包,可以在程序运行的过程中动态地生成对象和接口,CGLIB底层基于字节码处理框架ASM来转换字节码并生成新的类

1、引入依赖


    cglib
    cglib-nodep
    3.3.0

2、具体被代理对象

package com.example.demo.proxy;
public class CglibSubject {
	public void coreMethod() {
		System.out.println("具体角色");
	}
}

3、增强被代理对象

package com.example.demo.proxy;
public class Advice {
	public void before(){
		System.out.println("前置处理");
	}
	public void after(){
		System.out.println("后置处理");
	}
}

4、CGLIB动态代理测试

package com.example.demo.proxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class CglibTest {
	public static void main(String[] args) {
		// 被代理对象
		CglibSubject cglibSubject = new CglibSubject();
		// 增强对象
		Advice advice = new Advice();
		// 创建增强器
		Enhancer enhancer = new Enhancer();
		// 设置增强目标类
		enhancer.setSuperclass(cglibSubject.getClass());
		// 设置回调
		enhancer.setCallback(new MethodInterceptor() {
			@Override
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
					throws Throwable {
				advice.before();
				Object invoke = method.invoke(cglibSubject, args);
				advice.after();
				return invoke;
			}
		});
		// 创建代理,调用核心方法
		CglibSubject targetProxy = (CglibSubject) enhancer.create();
		targetProxy.coreMethod();
	}
}

三、Spring AOP动态代理

Spring 会根据当前被代理类是否实现了接口进行JDK动态代理和CGLIB动态代理的选择

详见DefaultAopProxyFactory类源码:

package org.springframework.aop.framework;
import java.io.Serializable;
import java.lang.reflect.Proxy;
import org.springframework.aop.SpringProxy;
import org.springframework.core.NativeDetector;
import org.springframework.util.ClassUtils;
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
    private static final long serialVersionUID = 7930414337282325166L;
    public DefaultAopProxyFactory() {
    }
    public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
        if (NativeDetector.inNativeImage() || !config.isOptimize() && !config.isProxyTargetClass() && !this.hasNoUserSuppliedProxyInterfaces(config)) {
            return new JdkDynamicAopProxy(config);
        } else {
            Class targetClass = config.getTargetClass();
            if (targetClass == null) {
                throw new AopConfigException("TargetSource cannot determine target class: Either an interface or a target is required for proxy creation.");
            } else {
                return (AopProxy)(!targetClass.isInterface() && !Proxy.isProxyClass(targetClass) && !ClassUtils.isLambdaClass(targetClass) ? 
                new ObjenesisCglibAopProxy(config) : new JdkDynamicAopProxy(config));
            }
        }
    }
    private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
        Class[] ifcs = config.getProxiedInterfaces();
        return ifcs.length == 0 || ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0]);
    }
}
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]