Jdk实现的动态代理
概念
代理的目标是在不修改现有代码的前提下对方法进行增强,实现在执行方法之前或之后进行某些操作,如日志记录、数据库事务操作等。
- 静态代理。由我们自己去定义一个代理类,去包含要被代理的类(以下称为目标类),并暴露和目标类一样的方法供外界调用,然后在这个方法里面调用目标类的具体方法,此时就可以在调用目标类的具体方法前后进行其他操作了。
- 动态代理。由jvm在程序运行期动态去创建代理类,这样就解决了静态代理的致命缺点:当目标类新增了要被代理的方法时需要再次修改代理类的代码。比较知名的动态代理实现主要有jdk与cglib两种,其中,jdk实现的动态代理要求目标类必须实现了某个接口才行,而cglib主要基于继承来实现动态代理故没有此约束。
使用jdk动态代理
使用jdk动态代理的代码是固定的,主要是理解其原理,代码如下:
MyInvocationHandler类
public class MyInvocationHandler implements InvocationHandler {
private Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before");
Object obj = method.invoke(target,args);
System.out.println("after");
return obj;
}
}
proxyTest类
public class proxyTest {
public static void main(String[] args) {
Service s = new ServiceImpl();
MyInvocationHandler handler = new MyInvocationHandler(s);
Service proxy = (Service)Proxy.newProxyInstance(s.getClass().getClassLoader(),s.getClass().getInterfaces(),handler);
int res = proxy.add(1,2);
//int res = proxy.sub(1,2);
System.out.println(res);
return;
}
}
interface Service {
int add(int a,int b);
int sub(int a,int b);
}
class ServiceImpl implements Service {
@Override
public int add(int a, int b) {
System.out.println("执行了加方法");
return a + b;
}
@Override
public int sub(int a, int b) {
System.out.println("执行了减方法");
return a - b;
}
}
当上面的程序执行到proxy.add(1,2)这一行时会跳转到自定义的MyInvocationHandler类中执行invoke方法,这也是最容易让人疑惑的地方,这个proxy到底是哪个类模板生成的对象?为什么会跳转执行invoke方法?
jdk为我们生成的代理类
下面是将 jdk为我们生成的代理类的字节码文件反编译得到的代码:
public final class $Proxy0 extends Proxy implements ServiceImpl {
private static Method m1;
private static Method m9;
private static Method m2;
private static Method m3;
private static Method m4;
private static Method m7;
private static Method m6;
private static Method m8;
private static Method m10;
private static Method m0;
private static Method m5;
public $Proxy0(InvocationHandler paramInvocationHandler) {
super(paramInvocationHandler);
}
public final boolean equals(Object paramObject) {
try {
return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void notify() {
try {
this.h.invoke(this, m9, null);
return;
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final String toString() {
try {
return (String)this.h.invoke(this, m2, null);
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final int add(int paramInt1, int paramInt2) {
try {
return ((Integer)this.h.invoke(this, m3, new Object[] { Integer.valueOf(paramInt1), Integer.valueOf(paramInt2) })).intValue();
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final int sub(int paramInt1, int paramInt2) {
try {
return ((Integer)this.h.invoke(this, m4, new Object[] { Integer.valueOf(paramInt1), Integer.valueOf(paramInt2) })).intValue();
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void wait(long paramLong) throws InterruptedException {
try {
this.h.invoke(this, m7, new Object[] { Long.valueOf(paramLong) });
return;
} catch (Error|RuntimeException|InterruptedException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void wait(long paramLong, int paramInt) throws InterruptedException {
try {
this.h.invoke(this, m6, new Object[] { Long.valueOf(paramLong), Integer.valueOf(paramInt) });
return;
} catch (Error|RuntimeException|InterruptedException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final Class getClass() {
try {
return (Class)this.h.invoke(this, m8, null);
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void notifyAll() {
try {
this.h.invoke(this, m10, null);
return;
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final int hashCode() {
try {
return ((Integer)this.h.invoke(this, m0, null)).intValue();
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void wait() throws InterruptedException {
try {
this.h.invoke(this, m5, null);
return;
} catch (Error|RuntimeException|InterruptedException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
m9 = Class.forName("com.zzz.reflect.ServiceImpl").getMethod("notify", new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m3 = Class.forName("com.zzz.reflect.ServiceImpl").getMethod("add", new Class[] { int.class, int.class });
m4 = Class.forName("com.zzz.reflect.ServiceImpl").getMethod("sub", new Class[] { int.class, int.class });
m7 = Class.forName("com.zzz.reflect.ServiceImpl").getMethod("wait", new Class[] { long.class });
m6 = Class.forName("com.zzz.reflect.ServiceImpl").getMethod("wait", new Class[] { long.class, int.class });
m8 = Class.forName("com.zzz.reflect.ServiceImpl").getMethod("getClass", new Class[0]);
m10 = Class.forName("com.zzz.reflect.ServiceImpl").getMethod("notifyAll", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
m5 = Class.forName("com.zzz.reflect.ServiceImpl").getMethod("wait", new Class[0]);
return;
} catch (NoSuchMethodException noSuchMethodException) {
throw new NoSuchMethodError(noSuchMethodException.getMessage());
} catch (ClassNotFoundException classNotFoundException) {
throw new NoClassDefFoundError(classNotFoundException.getMessage());
}
}
}
只看几个关键的地方。
- 这个类继承了java反射包下的Proxy类,与目标类一样实现了我们定义的ServiceImpl接口。
- 包含了很多Method对象,并在最下面的静态代码块中通过反射初始化,包括我们定义的接口中的两个方法。
- 实现的方法中都调用了h.invoke()方法,这个h是继承了Proxy类中而来,实际就是我们定义的MyInvocationHandler类。(调用Proxy.newProxyInstance()时传入的第三个参数)。
当我们通过代理对象调用add或sub方法时,就会执行上面代码中的同名方法,调用h.invoke()从而进入我们定义的MyInvocationHandler中的invoke方法(增强的内容都写在这里)。至于jdk是如何生成这个代理类模板的又是另一个故事了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)