通过一张图,先了解什么是代理模式
可以看到这种模式可以在调用方与目标对象之间起到中介作用,从而保护目标对象,并可以扩展目标对象的功能(ps:这里可以想想符合一种什么设计原则
)
JDK动态代理具体实现
JDK动态代理是基于接口的代理,只能对实现了接口的类进行代理
1.定义接口,实现动态代理的目标接口
java
//定义接口
public interface TargetInterFace {
void targetMethod();
}
java
//接口实现
public class TargetObject implements TargetInterFace{
@Override
public void targetMethod() {
System.out.println("/// "+this.getClass().getName()+" 执行");
}
}
2.实现InvocationHandler接口,重写invoke方法
InvocationHandler
是Java中的一个接口,是Java反射API的一部分,它用于动态创建接口的代理实例。
java
public class AopInvocation implements InvocationHandler {
private Object target;
public AopInvocation(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("/// "+this.getClass().getName()+" 执行前");
method.invoke(this.target, args);
System.out.println("/// "+this.getClass().getName()+" 执行后");
return null;
}
}
InvocationHandler
的主要作用是将方法调用与具体的实现解耦,使得在代理对象中可以灵活地处理方法调用。
这个接口只有一个方法,即invoke()
方法,它负责处理对代理对象的方法调用(当代理实例调用方法时,方法调用被编码分派到InvocationHandler的invoke方法)
通过使用InvocationHandler,可以更加灵活地扩展程序的功能(例如在调用前后添加额外的逻辑、对调用参数进行校验等),而不需要修改原有的代码
3.创建动态代理对象,将目标对象和InvocationHandler传递给Proxy.newProxyInstance方法。
Proxy
类是JDK提供的一个工具类,用于创建代理对象。它提供了一个静态方法newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
,用于创建代理对象。该方法需要三个参数:
ClassLoader loader
:指定代理对象的类加载器。Class<?>[] interfaces
:指定代理对象要实现的接口。InvocationHandler h
:指定实现了InvocationHandler
接口的对象,用于处理方法调用。
java
public static void main(String[] args) {
//目标对象
TargetObject targetObject = new TargetObject();
//建立代理与被代理对象的关系
AopInvocation aopInvocation=new AopInvocation(targetObject);
//生成代理对象
TargetInterFace instance = (TargetInterFace) Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), aopInvocation);
instance.targetMethod();
}

JDK动态代理原理
为什么没有显式调用invoke,但invoke方法确实被执行了?

通过Proxy.newProxyInstance
方法为大家解释
swift
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
/**
* 非核心代码省略
*/
/*
* 获取到代理接口的class
*/
Class<?> cl = getProxyClass0(loader, intfs);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
/**
* 用反射获取构造方法,然后创建代理类实例
*/
final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
/**
* 非核心代码省略
*/
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
}
}
通过getProxyClass
获取到代理接口的class($Proxy0)
通过Arthas反编译$Proxy0
的源码,可以看到该类继承了目标接口
然后通过用反射获取构造方法,然后创建代理类实例。
代理对象的方法调用都是通过super.h.invoke(this,m1,(Object[])null)
调用,其中的 super.h.invoke
实际上是在创建代理的时候传递给Proxy.newProxyInstance的 代理对象,它继承InvocationHandler类,负责实际的调用处理逻辑。 代理对象invoke()接收到method、args等参数后,然后通过反射让被代理的对象执行目标方法
总结
JDK动态代理符合我们的开闭原则。
JDK动态代理主要面向接口,可以为接口中的所有方法提供代理,没有实现接口的类无法进行代理。