您的位置:首页 > 编程语言 > Java开发

Java 动态代理实现及原理

2017-09-14 17:21 423 查看



什么是 Java动态代理?

Java 的动态代理,就是在程序运行的过程中,根据被代理的接口来动态生成代理类的class文件,并加载运行的过程。

在 java 的动态代理机制中,有两个重要的类或接口,一个是 InvocationHandler(Interface)、另一个则是 Proxy(Class),这一个类和接口是实现我们动态代理所必须用到的。首先我们先来看看java的API帮助文档是怎么样对这两个类进行描述的,首先看 InvocationHandler 接口:

InvocationHandler is the interface implemented by the invocation handler of a proxy instance.
Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.


每一个动态代理类都必须要实现 InvocationHandler 这个接口,并且每个代理类的实例都关联到了一个 handler,当我们通过代理对象调用一个方法的时候,这个方法的调用就会被转发为由 InvocationHandler 这个接口的 invoke 方法来进行调用。我们来看看 InvocationHandler 这个接口的唯一一个方法 invoke 方法,这个方法中的三个参数分别代表:

Object invoke(Object proxy, Method method, Object[] args) throws Throwable
proxy:指代我们所代理的那个真实对象
method: 指代的是我们所要调用真实对象的某个方法的Method对象
args: 指代的是调用真实对象某个方法时接受的参数


接下来我们来看看Proxy这个类:

Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.


Proxy 这个类的作用就是用来动态创建一个代理对象的类,它提供了许多的方法,但是我们用的最多的就是 newProxyInstance 这个方法:

public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,  InvocationHandler h)  throws IllegalArgumentException
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.


这个方法的作用就是得到一个动态的代理对象,其接收三个参数,我们来看看这三个参数所代表的含义:

public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
loader: 一个ClassLoader对象,定义了由哪个ClassLoader对象来对生成的代理对象进行加载
interfaces: 一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口,如果我提供了一组接口给它,那么这个代理对象就宣称实现了该接口(多态),这样我就能调用这组接口中的方法了
h: 一个InvocationHandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个InvocationHandler对象上


接下来通过一个实例,通过实例跟踪分析 JDK 源码可以帮助我们更好的理解 Java 的动态代理实现原理

创建一个接口(这个接口是要被代理的接口)

package demo.proxy.demo2;

public interface UserService {
public abstract void add();
}


创建接口的实现类

package demo.proxy.demo2;

public class UserServiceImpl implements UserService {
@Override
public void add() {
System. out.println("----- add -----" );
}
}


创建一个实现 java.lang.reflect.InvocationHandler 接口的代理类

package demo.proxy.demo2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
* 代理处理类
*/
public class MyInvocationHandler implements InvocationHandler {
private Object target ;
public MyInvocationHandler(Object target) {
super();
this.target = target;
}
public Object getProxy() {
/* 通过Proxy 的 newProxyInstance 方法来创建我们的代理对象,我们来看看其三个参数
* 第一个参数 Thread.currentThread().getContextClassLoader(),我们这里使用当前 handler 这个类的 ClassLoader 对象来加载我们的代理对象
* 第二个参数 this.target.getClass().getInterfaces(),为代理对象提供接口的是真实对象所实现的接口,表示代理的是该真实对象,这样就能调用这组接口中的方法了
* 第三个参数 this 当前类对象,将代理对象和当前 handler 关联在一块
*/
//             return Proxy.newProxyInstance(MyInvocationHandler.class.getContextClassLoader(), this.target.getClass().getInterfaces(), this);
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), this.target .getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy , Method method, Object[] args) throws Throwable {
System. out.println("-------- before --------" );
System. out.println("Method:" + method);
// 当代理对象调用真实对象的方法的时候,其会自动跳转到代理对象关联的 handler 的 invoke 方法来进行调用
Object result = method .invoke(this. target, args );
System. out.println("-------- after --------" );
return result ;
}
}


创建测试类使用代理

package demo.proxy.demo2;

import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
import sun.misc.ProxyGenerator;

public class DynamicProxyTest {
@Test
public void test() {
// 真实对象
UserService userService = new UserServiceImpl();
// 代理
MyInvocationHandler handler = new MyInvocationHandler( userService);
// 通过代理创建的代理对象
UserService proxyUserService = (UserService) handler .getProxy();
// 代理对象调用真实对象的方法
proxyUserService.add();
// 将产生的代理对象输出生成 class 文件,反编译进行查看,$Proxy0.class 这个 class 文件要先手动创建好,否则会抛出FileNotFoundException异常
String path = "D:/$Proxy0.class" ;
byte[] classFile = ProxyGenerator.generateProxyClass ("$Proxy0" , UserServiceImpl.class .getInterfaces());
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
out.write(classFile );
out.flush();
} catch (Exception e ) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e ) {
e.printStackTrace();
}
}
}
}


控制台输出结果

com.sun.proxy.$Proxy0
-------- before --------
Method:public abstract void demo.proxy.demo2.UserService.add()
----- add -----
-------- after --------


上面可以看出,JDK的动态代理使用起来非常简单,但是这个代理对象是由谁且怎么生成的?invoke 方法是怎么调用的? invoke 和 add 方法有什么对应关系?生成的代理对象是什么样子的?带着这些问题,我们看一下源码。这里采用的 JDK 是 jdk1.7.0_79 ,首先,我们的入口便是上面测试类里的 getProxy() 方法,跟进去,看这个方法

public Object getProxy() {
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), this.target .getClass().getInterfaces(), this);
}


也就是说,JDK 的动态代理,是通过一个叫 Proxy 的类来实现的,我们继续跟进去,看看 Proxy 类的 newProxyInstance() 方法,先看 JDK 的注释

Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.  This method is equivalent to:

@param loader, the class loader to define the proxy class(定义代理类的类装载器)
@param interfaces, the list of interfaces for the proxy class to implement(代理类实现的接口列表)
@param h, the invocation handler to dispatch method invocations to(调用处理程序(handler)调度方法调用)
@return a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces


根据 JDK 注释我们得知,newProxyInstance 方法最终将返回一个实现了指定接口的类的实例,其三个参数分别是:ClassLoader(装载齐类)、interfaces(代理类实现的接口)、handler(自己实现的 InvocationHandler 类)。通过下面几条关键的代码,看这个代理类的实例对象到底是怎么生成的。

@CallerSensitive
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces , InvocationHandler h ) throws IllegalArgumentException {
// 生成目标代理 class
Class<?> cl = getProxyClass0( loader, intfs );
...
// 得到该类的构造方法对象
final Constructor<?> cons = cl.getConstructor(constructorParams);
// 将我们实现的 InvocationHandler 赋值给当前的 ih
final InvocationHandler ih = h;
...
// 通过生成的构造对象和 handler 生成代理类的实例对象
return newInstance( cons, ih );
...
}


其中 newInstance 只是调用 Constructor.newInstance 来构造相应的代理类实例,详细内容如下

private static Object newInstance(Constructor<?> cons, InvocationHandler h) {
try {
return cons .newInstance(new Object[] { h} );
} catch (IllegalAccessException | InstantiationException e) {
throw new InternalError(e.toString());
} catch (InvocationTargetException e) {
Throwable t = e .getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t ;
} else {
throw new InternalError(t.toString());
}
}
}


接下来看 getProxyClass0 这个方法的实现

private static Class<?> getProxyClass0(ClassLoader loader, Class<?>... interfaces ) {
// 代理的接口数量
if (interfaces .length > 65535) {
throw new IllegalArgumentException( "interface limit exceeded" );
}
// JDK对代理进行了缓存,如果已经存在相应的代理类,则直接返回,否则才会通过ProxyClassFactory来创建代理
return proxyClassCache.get( loader, interfaces );
}


这里用到了缓存,先从缓存里查一下,如果存在,直接返回,不存在就新创建。其中代理缓存是使用WeakCache实现的,如下

private static final WeakCache<ClassLoader, Class<?>[], Class<?>>
proxyClassCache = new WeakCache<>( new KeyFactory(), new ProxyClassFactory());


在这个get方法里,主要看如下代码

public V get(K key, P parameter ) {
...
Object subKey = Objects.requireNonNull( subKeyFactory.apply(key , parameter));
...
}


此处提到了apply(),是 Proxy 类的内部类 ProxyClassFactory 实现 WeakCache 的内部接口 BiFunction 的 apply 方法,具体实现如下

private static final class ProxyClassFactory
implements BiFunction<ClassLoader, Class<?>[], Class<?>> {
// 代理类的前缀
private static final String proxyClassNamePrefix = "$Proxy";
// 用于生成代理类名字的计数器
private static final AtomicLong nextUniqueNumber = new AtomicLong();
@Override
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(生成的代理类的包名)
/*
* 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.
* 对于非公共接口,代理类的包名与接口的相同
*/
for (Class<?> intf : interfaces) {
int flags = intf.getModifiers();
if (!Modifier.isPublic( flags)) {
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) {
// 公共接口的包名默认用 com.sun.proxy
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
}
// 获取计数器
long num = nextUniqueNumber.getAndIncrement();
// 默认情况下,代理类的完全限定名为:com.sun.proxy.$Proxy0,com.sun.proxy.$Proxy1……依次递增
String proxyName = proxyPkg + proxyClassNamePrefix + num;
// 生成 class 字节码文件
byte[] proxyClassFile = ProxyGenerator.generateProxyClass (
proxyName, interfaces );
try {
// 根据二进制字节码返回相应的Class实例
return defineClass0( loader,  proxyName, proxyClassFile, 0, proxyClassFile.length);
} catch (ClassFormatError e ) {
throw new IllegalArgumentException( e.toString());
}
}
}


ProxyGenerator 是 sun.misc 包中的类,这个包没有开源,只能反编译之后来看

public static byte[] generateProxyClass(String s, Class aclass[]) {
ProxyGenerator proxygenerator = new ProxyGenerator( s, aclass);
byte abyte0 [] = proxygenerator.generateClassFile();
// 这里根据参数配置,决定是否把生成的字节码(.class文件)保存到本地磁盘,我们可以通过把相应的class文件保存到本地,再反编译来看看具体的实现,这样更直观
if(saveGeneratedFiles)
AccessController.doPrivileged(new PrivilegedAction(s, abyte0) {
public Void run() {
try {
FileOutputStream fileoutputstream = new FileOutputStream((new StringBuilder()).append(ProxyGenerator.dotToSlash(name)).append(".class" ).toString());
fileoutputstream.write(classFile);
fileoutputstream.close();
return null;
} catch (IOException ioexception) {
throw new InternalError(( new StringBuilder()).append("I/O exception saving generated file: ").append(ioexception).toString());
}
}
public volatile Object run() {
return run();
}
final String val$name;
final byte val$classFile[];
{
name = s;
classFile = abyte0;
super();
}
}) ;
return abyte0 ;
}


saveGeneratedFiles 这个属性的值从哪里来呢:

private static final boolean saveGeneratedFiles = ((Boolean)AccessController.doPrivileged(new GetBooleanAction("sun.misc.ProxyGenerator.saveGeneratedFiles" ))).booleanValue();


GetBooleanAction 实际上是调用 Boolean.getBoolean(propName) 来获得的,而 Boolean.getBoolean(propName) 调用了 System.getProperty(name),所以我们可以设置 sun.misc.ProxyGenerator.saveGeneratedFiles 这个系统属性为true 来把生成的class保存到本地文件来查看。对于生成的代理对象是什么样子的?问题,看下面反编译后的代码

import demo.proxy.demo2.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 m3;
private static Method m0;
private static Method m2;

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 localError) {
throw localError ;
} catch (Throwable localThrowable ) {
throw new UndeclaredThrowableException( localThrowable);
}
}

public final void add(){
try {
this.h.invoke(this, m3, null);
return;
} catch (Error | RuntimeException localError) {
throw localError;
} catch (Throwable localThrowable ) {
throw new UndeclaredThrowableException( localThrowable);
}
}

public final int hashCode(){
try {
return ((Integer) this.h.invoke( this, m0, null)).intValue();
} catch (Error | RuntimeException localError) {
throw localError ;
} catch (Throwable localThrowable ) {
throw new UndeclaredThrowableException( localThrowable);
}
}

public final String toString(){
try {
return (String) this.h.invoke( this, m2, null);
} catch (Error | RuntimeException localError) {
throw localError ;
} catch (Throwable localThrowable ) {
throw new UndeclaredThrowableException( localThrowable);
}
}

static {
try {
m0 = Class.forName ("java.lang.Object").getMethod("hashCode", new Class[0]);
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 ("demo.proxy.demo2.UserService").getMethod("add", new Class[0]);
return;
} catch (NoSuchMethodException localNoSuchMethodException) {
throw new NoSuchMethodError(localNoSuchMethodException .getMessage());
} catch (ClassNotFoundException localClassNotFoundException) {
throw new NoClassDefFoundError(localClassNotFoundException .getMessage());
}
}
}


可以看到生成的代理类有如下内容:

1、继承了 Proxy 类,实现了代理的接口,由于 java 不能多继承,这里已经继承了 Proxy 类了,不能再继承其他的类,所以 JDK 的动态代理不支持对实现类的代理,只支持接口的代理。

2、提供了一个使用 InvocationHandler 作为参数的构造方法。

3、生成静态代码块来初始化接口中方法的 Method 对象,以及 Object 类的 equals、hashCode、toString 方法。

4、重写了 Object 类的 equals、hashCode、toString,它们都只是简单的调用了 InvocationHandler 的 invoke 方法,即可以对其进行特殊的操作,也就是说 JDK 的动态代理还可以代理上述三个方法。

5、代理类实现代理接口的 add 方法中,只是简单的调用了 InvocationHandler 的 invoke 方法,我们可以在 invoke 方法中进行一些特殊操作,甚至不调用实现的方法,直接返回

通过上面的代码可以看出测试方法里的 proxyUserService.add(),此处的 add() 方法,就已经不是原始的 UserService 里的 add() 方法了,而是新生成的代理类的 add() 方法。核心就在于 this.h.invoke(this. m3, null); 此处的 h 是啥呢?我们看看这个类的类名 public final class $Proxy0 extends Proxy implements UserService 发现这个新生的类继承了
Proxy 类和实现了 UserService 接口,而 h 是在构造方法里传入了一个 InvocationHandler 类型的参数,这个参数在什么时候传入的呢?是在 Constructor.newInstance 的时候通过 return cons .newInstance(new Object[]
{h})  的时候产生对象并把
h(我们自己实现的 MyInvocationHandler 对象)传入代理对象中的。所以不难发现 proxyUserService.add() 调用的其实是 MyInvocationHandler 中的 invoke() 方法,而 invoke() 方法内的 m3 就是实际的 UserService 对象的 add() 方法。

代理对象是由谁且怎么生成的?

由 Proxy 类 getProxyClass0 这个方法生成目标代理类,然后得到该类的构造方法,通过反射产生代理对象的实例对象。

invoke 方法是怎么调用的?

在生成的代理对象中要执行的目标方法内通过 MyInvocationHandler 对象调用

invoke 和 add 方法有什么对应关系?

生成的代理对象中的 add 方法调用自己实现的 InvocationHandler 对象中的 invoke 方法,InvocationHandler 对象中的 invoke 方法又调用实际对象的 add 方法

参考文章
http://blog.csdn.net/zhangerqing/article/details/42504281/ http://blog.csdn.net/mhmyqn/article/details/48474815 http://www.cnblogs.com/xiaoluo501395377/p/3383130.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java动态代理 java