聊聊jdk动态代理,虽然是老生常谈的话了。首先,以demo入手
1、demo实现如下
接口类 IUserDao
package pers.growing.proxy.jdk;
public interface IUserDao {
void save();
}
接口实现类
package pers.growing.proxy.jdk;
public class UserDao implements IUserDao {
public void save() {
System.out.println("保存操作");
}
}
代理类实现
package pers.growing.proxy.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactory implements InvocationHandler {
private Class<?> target; //接口
private Object real; //接口实现类
public ProxyFactory(Class<?> target) {
this.target = target;
}
public Object bind(Object real) {
this.real = real;
return Proxy.newProxyInstance(target.getClassLoader(), new Class[]{target}, this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before save");
Object res = method.invoke(real, args);
System.out.println("after save");
return res;
}
}
测试方法
package pers.growing.proxy.jdk;
import sun.misc.ProxyGenerator;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Modifier;
public class Test {
public static void main(String[] args) throws Exception {
IUserDao proxy = (IUserDao) new ProxyFactory(IUserDao.class).bind(new UserDao());
proxy.save();
/**
* jdk动态代理生成的文件
*/
byte[] proxyClass = ProxyGenerator.generateProxyClass(proxy.getClass().getSimpleName(), proxy.getClass().getInterfaces(), Modifier.PUBLIC | Modifier.FINAL);
String fileName=proxy.getClass().getSimpleName()+".class";
System.out.println("生成文件名为:"+fileName);
FileOutputStream outputStream = new FileOutputStream(new File("D:\\"+fileName)); //存到d盘
outputStream.write(proxyClass);
outputStream.flush();
outputStream.close();
}
}
测试结果
before save
保存操作
after save
生成文件名为:$Proxy0.class
$Proxy0.class文件反编译后的内容如下
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import pers.growing.proxy.jdk.IUserDao;
public final class $Proxy0 extends Proxy
implements IUserDao
{
private static Method m1;
private static Method m3;
private static Method m2;
private static Method m0;
public $Proxy0(InvocationHandler paramInvocationHandler)
throws
{
super(paramInvocationHandler);
}
public final boolean equals(Object paramObject)
throws
{
try
{
return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final void save()
throws
{
try
{
this.h.invoke(this, m3, null);
return;
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final String toString()
throws
{
try
{
return (String)this.h.invoke(this, m2, null);
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final int hashCode()
throws
{
try
{
return ((Integer)this.h.invoke(this, m0, null)).intValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
static
{
try
{
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
m3 = Class.forName("pers.growing.proxy.jdk.IUserDao").getMethod("save", new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
return;
}
catch (NoSuchMethodException localNoSuchMethodException)
{
throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
}
catch (ClassNotFoundException localClassNotFoundException)
{
}
throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
}
}
save()
throws
{
try
{
this.h.invoke(this, m3, null);
return;
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final String toString()
throws
{
try
{
return (String)this.h.invoke(this, m2, null);
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final int hashCode()
throws
{
try
{
return ((Integer)this.h.invoke(this, m0, null)).intValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
static
{
try
{
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
m3 = Class.forName("pers.growing.proxy.jdk.IUserDao").getMethod("save", new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
return;
}
catch (NoSuchMethodException localNoSuchMethodException)
{
throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
}
catch (ClassNotFoundException localClassNotFoundException)
{
}
throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
}
}
2、大概流程为(绿色字体:类#方法):
3、总结
jdk动态代理理解原理后还是挺简单的,虽然具体的细节实现还是挺繁琐的,特别是ProxyGenerator#generateProxyClass这块逻辑还是挺复杂的哟,不过我只要知道原理就够用了。注:jdk动态代理只能代理有接口的实现类