JDK动态代理实现方式
在Spring框架中经典的AOP就是通过动态代理来实现的,Spring分别采用了JDK的动态代理和Cglib动态代理,本文就来分析一下JDK是如何实现动态代理的。
在分析源码之前我们先通过一个简单的例子看看JDK是如何实现动态代理的。
JDK的动态代理是基于接口实现的,所以我们被代理的对象必须有一个接口(后面我们会分析为什么是基于接口实现的)
public interface UserService {
/**
* 显示一下用户信息
* @param userId
*/
public void displayUser(String userId);
}
然后实现类实现UserService接口
public class UserServiceImpl implements UserService {
@Override
public void displayUser(String userId) {
System.out.println("display:"+userId);
}
}
关键的一步是实现InvocationHandler
public class MyInvocationHandler implements InvocationHandler {
//被代理对象,java代理模式的一个必要要素就是代理对象要能拿到被代理对象的引用
private Object target;
public MyInvocationHandler(){ }
public MyInvocationHandler(Object target){
this.target=target;
} /**
* 回调方法
* @param proxy JDK生成的代理对象
* @param method 被代理的方法
* @param args 被代理方法的参数
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("执行前...");
Object object= method.invoke(target,args);
System.out.println("执行后...");
return object;
}
}
invoke方法第一个参数是JDK生成的代理对象,这个参数需要注意。因为在invoke中调用proxy的hashcode()、equals()、toString()、 method.invoke(proxy,args);都会递归调用invoke方法出现死循环。后边我们通过源码来看一下原因。
最后我们来写一个测试类测试一下:
public class Test {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
UserService userService = (UserService) Proxy.newProxyInstance(UserService.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), new MyInvocationHandler(new UserServiceImpl()));
userService.displayUser("234");
System.out.println(userService.toString());
}
}
通过Proxy.newProxyInstance可以获得一个代理对象,它实现了UserService接口,打印一下地址我们可以看到确实是一个代理类:
执行前...
display:234
执行后...
class com.sun.proxy.$Proxy0
源码分析
Proxy.newProxyInstance
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
//校验InvocationHandler不能为空
Objects.requireNonNull(h); final Class<?>[] intfs = interfaces.clone();
final SecurityManager sm = System.getSecurityManager();
//进行权限校验,非关键代码可以不用关心
if (sm != null) {
checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
} /*
* Look up or generate the designated proxy class.
*/
//这个是关键代码,根据classloader、interfaces生成代理类字节码,直接生成的.class不是生成.java编译的
Class<?> cl = getProxyClass0(loader, intfs); /*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (sm != null) {
checkNewProxyPermission(Reflection.getCallerClass(), cl);
}
//获取代理类的构造器(构造器入参:{InvocationHandler.class})
final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
//如果代理类是不可访问的,通过特权将它设置它的构造器为可访问的
if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
//通过构造器生成代理对象并返回
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new InternalError(t.toString(), t);
}
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString(), e);
}
}
这个方法的代码量比较少,总结一下分为三步:
第一步:通过classloader、interfaces获取代理类Class;
第二步:通过代理类Class获取入参为{InvocationHandler}的构造器;
第三步:通过构造器实例化代理对象。
其中,第一步是最关键的代码,我再深入getProxyClass0方法看看具体都干了什么
private static Class<?> getProxyClass0(ClassLoader loader,Class<?>... interfaces) {
//接口数不能大于65535
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
} // If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
//如果代理类已存在与缓存中直接获取,如果不存在通过ProxyClassFactory生成并返回
return proxyClassCache.get(loader, interfaces);
}
我们这里主要关心代理类是如何生成的,缓存的机制我们先略过,回头可以再开一篇分析一下缓存机制。我们这里只要知道如果缓存中没有对应的代理类就调用ProxyClassFactory的apply方法生成。
public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
for (Class<?> intf : interfaces) {
/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
Class<?> interfaceClass = null;
try {
interfaceClass = Class.forName(intf.getName(), false, loader);
} catch (ClassNotFoundException e) {
}
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
/*
* Verify that the Class object actually represents an
* interface.
*/
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
/*
* Verify that this interface is not a duplicate.
*/
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
} String proxyPkg = null; // package to define proxy class in
int accessFlags = Modifier.PUBLIC | Modifier.FINAL; /*
* Record the package of a non-public proxy interface so that the
* proxy class will be defined in the same package. Verify that
* all non-public proxy interfaces are in the same package.
*/
//如果代理类实现的接口是否非public的,代理类和它实现的接口必须在一个包下
for (Class<?> intf : interfaces) {
int flags = intf.getModifiers();
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
} if (proxyPkg == null) {
// if no non-public proxy interfaces, use com.sun.proxy package
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
} /*
* Choose a name for the proxy class to generate.
*/
long num = nextUniqueNumber.getAndIncrement();
//代理类类名com.sun.proxy.$Proxy0
String proxyName = proxyPkg + proxyClassNamePrefix + num; /*
* Generate the specified proxy class.
*/
//生成class文件
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
//生成Class并加载到JVM
return defineClass0(loader, proxyName,
proxyClassFile, 0, proxyClassFile.length);
} catch (ClassFormatError e) {
/*
* A ClassFormatError here means that (barring bugs in the
* proxy class generation code) there was some other
* invalid aspect of the arguments supplied to the proxy
* class creation (such as virtual machine limitations
* exceeded).
*/
throw new IllegalArgumentException(e.toString());
}
}
}
ProxyClassFactory.apply方法总结一下就是以下步骤:
第一步:校验入参Class<?> interfaces,包括classloader加载的interfaceClass是否与入参是同一个Object、interfaceClass必须是接口类型、interfaceClass集合中不能重复(通过IdentityHashMap的特性,key值相等比的是地址,即key1=key2)。
第二步:定义代理类的包名和类名。包名规则:如果接口不是public修饰的,接口必须在同一个包下,否则会抛异常。如果接口不是public修饰的,代理类包名与接口包名相同,否则默认包名为com.sun.proxy。类名规则:$Proxy+incrementNum。
第三步:生成代理类class文件。
第四步:调用native方法defineClass0方法生成Class类并加载到JVM。
我们通过ProxyGenerator.generateProxyClass生成代理类Class写入本地文件并反编译:
public class Test {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
byte[] proxyClass = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{UserService.class});
FileOutputStream outputStream = new FileOutputStream(new File("d:\\$Proxy0.class"));
outputStream.write(proxyClass);
outputStream.flush();
outputStream.close();
}
}
$Proxy0.java
import com.demo.proxy.jdk.UserService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy0 extends Proxy
implements UserService
{
private static Method m1;
private static Method m2;
private static Method m3;
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 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 void displayUser(String paramString)
throws
{
try
{
this.h.invoke(this, m3, new Object[] { paramString });
return;
}
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") });
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m3 = Class.forName("com.demo.proxy.jdk.UserService").getMethod("displayUser", new Class[] { Class.forName("java.lang.String") });
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());
}
}
}
我们可以看到代理类$Proxy0继承了Proxy类(JAVA只能单继承),所以JDK的动态代理只能基于接口。
首先,代理类会通过静态代码块初始化hashCode()、equals()、toString()这三个继承Object的方法,以及实现接口的方法。
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
static
{
try
{
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m3 = Class.forName("com.demo.proxy.jdk.UserService").getMethod("displayUser", new Class[] { Class.forName("java.lang.String") });
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());
}
}
然后,构造方法实例化代理类,入参为InvocationHandler这是父类Proxy的属性InvocationHandler h;
public $Proxy0(InvocationHandler paramInvocationHandler)
throws
{
super(paramInvocationHandler);
}
最后,覆盖接口方法,方法中调用InvocationHandler的invoke方法,从而实现了动态代理。
public final void displayUser(String paramString)
throws
{
try
{
this.h.invoke(this, m3, new Object[] { paramString });
return;
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
/**
* 回调方法
* @param proxy JDK生成的代理对象
* @param method 被代理的方法
* @param args 被代理方法的参数
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("执行前...");
Object object= method.invoke(target,args);
System.out.println("执行后...");
return object;
}
到此,我们基本上了解JDK动态代理实现的原理。