Java动态代理机制的出现,使得 Java 开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象,便能动态地获得代理类。代理类会负责将所有的方法调用分派到委托对象上反射执行,在分派执行的过程中,开发人员还可以按需调整委托类对象及其功能,这是一套非常灵活有弹性的代理框架。下面我们开始动态代理的学习。
目录导航
动态代理的简要说明
在java的动态代理机制中,有两个重要的类或接口,一个是 InvocationHandler(Interface)、另一个则是 Proxy(Class)。
一、 InvocationHandler(interface)的描述:
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
这个方法接收三个参数和返回一个Object类型,它们分别代表的意思如下:
- proxy: 指代我们所代理的那个真实对象
- method: 指代的是我们所要调用真实对象的方法的Method对象
- args: 指代的是调用真实对象某个方法时接受的参数
返回的Object是指真实对象方法的返回类型,以上会在接下来的例子中加以深入理解。
the value to return from the method invocation on the proxy instance.
二、 Proxy(Class)的描述:
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
参数的理解:
// 一个ClassLoader对象,定义了由哪个ClassLoader对象来对生成的代理对象进行加载
loader - the class loader to define the proxy class
// 一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口
interfaces - the list of interfaces for the proxy class to implement
// 一个InvocationHandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个InvocationHandler对象上
h - the invocation handler to dispatch method invocations to
返回结果的理解: 一个代理对象的实例
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
简单的Java代理
我们创建一个Java项目用于对动态代理的测试与理解,项目结构如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQcAAADiCAIAAADMN5V1AAAbz0lEQVR4nO2d+1sUV56H+Tv2x3123cw4uzPGRE07MiaomIwRNY/xEjNBjZ2AJJnHSwQjICoIxEtHYKKIgnJr0UC8tahBg0ZRaWNM4iU2GgEN0FxURG0cTe0P1VSfa3U1VHVVV33fp548dnedqlPkvFXndNWnT5QAANqQueiFjR+/eO1ircw6GzZsCFt9lBOldwUASwNWAEBkAFYAAAlYAQAkYAUAkKhmRVfP/W2lNXk7q/J2VhWV1Tzse4R+2tbz8J31NWrtCwA0RTUrHIWV7Xdvdnc0d3Xcvn7lh7xtpeindzp7Yz4t5ZUFAEOhghUd3u416/OunVz7qGlzf9PGx1fWe0+nbstJWb4qd/mq3DXr8zq83YazwuOwRdkcHr2rARgSFaxYsz6v97c64cEpxvL8yfN/P7537wHfCo/DFoVgdw29QkqQswKrUrgqBBgIFaxYkZ4v9BxnL4IgDifkrQi0PJfdAA3R47ANCAOXFEuiyIqTJ09mZ2fzPl26aofg3c9efn8e82npUffNrMrv5m84wCqNW8F6I+wgVgiCyw5aWI7gVtTV1Q0fPnzYsGG8FRYv2yXcrRCX563lx2vKVq9zfrK09JOlpVev3T3QcGP80t2TksvPX79bvf8SVZqWQHwHP0v726bLHmVzOOxi3ybwKdLlGdiUyx5lc7j879td/qtQoBS224HP/J8yrRALuOyBTQRKSbv1OGzSv132qCi7g3UUgNEJYoWoxOzZs2WseNdeIjQXCc1FT27sSF3jzPv6yv7m5+JyqOV5t+93cbWrHU8+WPIVVZpnBdaGXHZxHaSDFbDG47APNDWXfeBjpPmKrTdQyu7CduuyRxFtldmDEs3DRMKaOyKD5K9HYB4FYHTkrJCUmDNnjowV0+btEm5+8fjaln+m7Pm8+mbVtX562X66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pCW4vq86DRSsrBAUp6pb2e5OKM9Bl8q6MN/dlvLkvw1M4xXtw+LUt4/paZH+2jD2uIFP6yoYVeIyHyDfLpdfwAlh+Rz7Jzdsa8QYv8E2kt5lBaqosL7ctCIKcFa3uqoGmdiXXPwxAP80fUIVezf9vYvCAjyukMz2DYFYQGhjcCkFBinr6vtXJtauI5dXyFR2ni27tHNVc9ufm6s/k9sS6VuAnf6XdDyyNzcg3k1FPMr6JY3N4FOSQyP3LXivoDzimYicAOrXHyW0LghCsB4UMcMX2PXCSRrv1jNXw9/0CMNu6NChHzv1BrKA+NbgVSlLU//OvhaOLPiKW/85f8PRBx6W1Y7pq/+BOG/Os/xGvOPL/nIzw410IhRXHzrGsfLPY6qivhthBnvBawQ5Sk5vl5rYFQaCsCPTXvU5n6kB/CXHgZlWss761pz4B+YixGoLUm1KpB0XvxchWKElRf/vZLGfKdOby7arZP2+ZeWfP8J82juk4f4CzAe74AflAwWib+bsAVL7ZQWrG81CKXKtpBSvwzf1NA+469HHh4FYgIwekqfXUJwQuAl6nMz/3aP7AqZ25mtfpDHSQpDHAYK3wOp3IlUTUElvdyFYo+Q6qNmnCD+v/Ti8X175emzShvaH65w2jWyr+eDHnLbosNRZgnFClAQHzRga+KmPcgf9GAOOmhr8gd6QbshVYNYnzPSPwzTpksXQgSM04KRDHhSJZQd64QIYBzqpc5PTc6s4Pvhp6b4FzvwLtMgn01gJlUStwQ/wY1gqF9ysO2qMvZb5OL40Zkw/ao58+vHfqk5Hdx1749qORfb/d0qKe8pj15ytljoscV0QihrVCITumDiub8yJjmTVix9RhgiBcciw6u+LFo/H/d+vQznBXzqyPWcgeF3WJiDAMfW9bIQ/aWmQWQRDu3/yptb7Gd68zzBVj55sjH7Mel+oY7jkoANAdsAIASMAKACABKwCABKwAAJL/B24nyquknb4HAAAAAElFTkSuQmCC" alt="" />
一、 先定义一个接口Interface,添加两个方法。
package com.huhx.proxy; public interface Interface {
void getMyName(); String getNameById(String id);
}
二、 定义一个真实的实现上述接口的类,RealObject:
package com.huhx.proxy; public class RealObject implements Interface {
@Override
public void getMyName() {
System.out.println("my name is huhx");
} @Override
public String getNameById(String id) {
System.out.println("argument id: " + id);
return "huhx";
}
}
三、 定义一个代理对象,也实现了上述的Interface接口:
package com.huhx.proxy; public class SimpleProxy implements Interface {
private Interface proxied; public SimpleProxy(Interface proxied) {
this.proxied = proxied;
} @Override
public void getMyName() {
System.out.println("proxy getmyname");
proxied.getMyName();
} @Override
public String getNameById(String id) {
System.out.println("proxy getnamebyid");
return proxied.getNameById(id);
}
}
四、 SimpleMain在Main方法中,测试上述的结果:
package com.huhx.proxy; public class SimpleMain {
private static void consume(Interface iface) {
iface.getMyName();
String name = iface.getNameById("1");
System.out.println("name: " + name);
} public static void main(String[] args) {
consume(new RealObject());
System.out.println("========================================================");
consume(new SimpleProxy(new RealObject()));
}
}
五、 运行的结果如下:
my name is huhx
argument id: 1
name: huhx
========================================================
proxy getmyname
my name is huhx
proxy getnamebyid
argument id: 1
name: huhx
Java的动态代理
完成了上述简单的Java代理,现在我们开始学习Java的动态代理,它比代理的思想更向前一步,因为它可以动态地创建代理并动态的处理对所代理方法的调用。在动态代理上所做的所有调用都会被重定向到单一的调用处理器上,它的工作是揭示调用的类型并确定相应的对策。下面我们通过案例来加深Java动态代理的理解:
一、 创建一个继承了InvocationHandler的处理器:DynamicProxyHandler
package com.huhx.dynamicproxy; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays; public class DynamicProxyHandler implements InvocationHandler {
private Object proxied; public DynamicProxyHandler(Object proxied) {
System.out.println("dynamic proxy handler constuctor: " + proxied.getClass());
this.proxied = proxied;
} @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("dynamic proxy name: " + proxy.getClass());
System.out.println("method: " + method.getName());
System.out.println("args: " + Arrays.toString(args)); Object invokeObject = method.invoke(proxied, args);
if (invokeObject != null) {
System.out.println("invoke object: " + invokeObject.getClass());
} else {
System.out.println("invoke object is null");
}
return invokeObject;
}
}
二、 我们写一个测试的Main方法,DynamicProxyMain:
package com.huhx.dynamicproxy; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy; import com.huhx.proxy.Interface;
import com.huhx.proxy.RealObject; public class DynamicProxyMain {
public static void consumer(Interface iface) {
iface.getMyName();
String name = iface.getNameById("1");
System.out.println("name: " + name);
} public static void main(String[] args) throws Exception, SecurityException, Throwable {
RealObject realObject = new RealObject();
consumer(realObject);
System.out.println("=============================="); // 动态代理
ClassLoader classLoader = Interface.class.getClassLoader();
Class<?>[] interfaces = new Class[] { Interface.class };
InvocationHandler handler = new DynamicProxyHandler(realObject);
Interface proxy = (Interface) Proxy.newProxyInstance(classLoader, interfaces, handler); System.out.println("in dynamicproxyMain proxy: " + proxy.getClass());
consumer(proxy);
}
}
三、 运行结果如下:
my name is huhx
argument id: 1
name: huhx
==============================
dynamic proxy handler constuctor: class com.huhx.proxy.RealObject
in dynamicproxyMain proxy: class com.sun.proxy.$Proxy0
dynamic proxy name: class com.sun.proxy.$Proxy0
method: getMyName
args: null
my name is huhx
invoke object is null
dynamic proxy name: class com.sun.proxy.$Proxy0
method: getNameById
args: [1]
argument id: 1
invoke object: class java.lang.String
name: huhx
从以上输出结果,我们可以得出以下结论:
- 与代理对象相关联的InvocationHandler,只有在代理对象调用方法时,才会执行它的invoke方法
- invoke的三个参数的理解:Object proxy是代理的对象, Method method是真实对象中调用方法的Method类, Object[] args是真实对象中调用方法的参数
Java动态代理的原理
一、 动态代理的关键代码就是Proxy.newProxyInstance(classLoader, interfaces, handler),我们跟进源代码看看:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException {
// handler不能为空
if (h == null) {
throw new NullPointerException();
} 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.
*/
// 通过loader和接口,得到代理的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;
if (sm != null && ProxyAccessHelper.needsNewInstanceCheck(cl)) {
// create proxy instance with doPrivilege as the proxy class may
// implement non-public interfaces that requires a special permission
return AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return newInstance(cons, ih);
}
});
} else {
// 创建代理对象的实例
return newInstance(cons, ih);
}
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString());
}
}
二、 我们看一下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());
}
}
}
三、 当我们通过代理对象调用 一个方法的时候,这个方法的调用就会被转发为由InvocationHandler这个接口的 invoke 方法来进行调用。
体现这句话的代码,我在源码中没有找到,于是我在测试类的main方法中加入以下代码:
if (proxy instanceof Proxy) {
InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy);
invocationHandler.invoke(proxy, realObject.getClass().getMethod("getMyName"), null);
System.out.println("--------------------------------------");
}
这段代码的输出结果如下,与上述中调用代理对象中的getMyName方法输出是一样的,不知道Jvm底层是否是这样判断的:
dynamic proxy handler constuctor: class com.huhx.proxy.RealObject
dynamic proxy name: class com.sun.proxy.$Proxy0
method: getMyName
args: null
my name is huhx
invoke object is null
--------------------------------------
友情链接
- 测试动态代理的源代码下载 访问密码 7d08