本篇博文介绍的是JDK的动态代理,Java中动态代理不仅仅是JDK的动态代理还有CGLIB动态代理。
(JDK)动态代理是指客户通过代理类来调用其它对象的方法,并且是在程序运行时根据需要动态创建目标类的代理对象。
即,不直接调用目标对象而是通过代理对象调用;代理对象不直接生成,而是程序运行时根据需要动态生成!
代理设计模式的原理:
使用一个代理将对象包装起来, 然后用该代理对象取代原始对象. 任何对原始对象的调用都要通过代理. 代理对象决定是否以及何时将方法调用转到原始对象上。
【1】Proxy
专门完成代理的操作类,是所有动态代理类的父类。通过此类为一个或多个接口动态地生成实现类。
提供了许多用于创建动态代理类和动态代理对象的静态方法。
① static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces)
创建一个动态代理类所对应的Class对象。
② static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
直接创建一个动态代理对象。
其他方法如下图:
【2】InvocationHandler
InvocationHandler 是一个接口,官方文档解释说,每个代理的实例都有一个与之关联的 InvocationHandler 实现类,如果代理的方法被调用,那么代理便会通知和转发给内部的 InvocationHandler 实现类,由它决定处理。
InvocationHandler接口源码如下:
/**
* {@code InvocationHandler} is the interface implemented by
* the <i>invocation handler</i> of a proxy instance.
* //InvocationHandler是一个被代理实例的调用处理程序实现的接口。
*
* <p>Each proxy instance has an associated invocation handler.
* // 每一个代理实例都有一个相关联的invocation handler
* When a method is invoked on a proxy instance, the method
* invocation is encoded and dispatched to the {@code invoke}
* method of its invocation handler.
* //当代理实例方法被调用时,代理会转发到相关联的invocation handler的invoke方法。
*
*/
public interface InvocationHandler {
/**
* Processes a method invocation on a proxy instance and returns
* the result.
* //处理代理实例上的方法调用并返回结果
* This method will be invoked on an invocation handler
* when a method is invoked on a proxy instance that it is
* associated with.
*// 当一个代理示例的方法被调用时,与代理实例相关联的invocation handler的invoke方法将会被调用。
* @param proxy the proxy instance that the method was invoked on
*
* @param method the {@code Method} instance corresponding to
* the interface method invoked on the proxy instance. The declaring
* class of the {@code Method} object will be the interface that
* the method was declared in, which may be a superinterface of the
* proxy interface that the proxy class inherits the method through.
*
* @param args an array of objects containing the values of the
* arguments passed in the method invocation on the proxy instance,
* or {@code null} if interface method takes no arguments.
* Arguments of primitive types are wrapped in instances of the
* appropriate primitive wrapper class, such as
* {@code java.lang.Integer} or {@code java.lang.Boolean}.
*
* @return the value to return from the method invocation on the
* proxy instance. If the declared return type of the interface
* method is a primitive type, then the value returned by
* this method must be an instance of the corresponding primitive
* wrapper class; otherwise, it must be a type assignable to the
* declared return type. If the value returned by this method is
* {@code null} and the interface method's return type is
* primitive, then a {@code NullPointerException} will be
* thrown by the method invocation on the proxy instance. If the
* value returned by this method is otherwise not compatible with
* the interface method's declared return type as described above,
* a {@code ClassCastException} will be thrown by the method
* invocation on the proxy instance.
*
* @throws Throwable the exception to throw from the method
* invocation on the proxy instance. The exception's type must be
* assignable either to any of the exception types declared in the
* {@code throws} clause of the interface method or to the
* unchecked exception types {@code java.lang.RuntimeException}
* or {@code java.lang.Error}. If a checked exception is
* thrown by this method that is not assignable to any of the
* exception types declared in the {@code throws} clause of
* the interface method, then an
* {@link UndeclaredThrowableException} containing the
* exception that was thrown by this method will be thrown by the
* method invocation on the proxy instance.
*
* @see UndeclaredThrowableException
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
}
InvocationHandler 内部只有一个 invoke() 方法,正是这个方法决定了怎么样处理代理传递过来的方法调用。
- proxy 代理对象
- method 代理对象调用的方法
- args 调用的方法中的参数
因为,Proxy 动态产生的代理对象会调用 InvocationHandler 实现类,所以 InvocationHandler 是实际执行者。
故而,总的运行流程为:代理示例.方法 –> InvocationHandler.invoke() –> 接口真正实现类的方法。
【3】动态代理步骤
① 父接口
父接口Human定义了两个方法:
interface Human {
void info();
void fly();
}
② 实现类SuperMan(被代理 类)
// 被代理类
class SuperMan implements Human {
public void info() {
System.out.println("我是超人!我怕谁!");
}
public void fly() {
System.out.println("I believe I can fly!");
}
}
③ InvocationHandler实现类
class MyInvocationHandler implements InvocationHandler {
// 被代理类对象的声明
Object obj;
// 动态的创建一个代理类的对象
public Object bind(Object obj){
this.obj = obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object returnVal = method.invoke(obj, args);
return returnVal;
}
}
④ 测试方法
public static void main(String[] args) {
//创建一个被代理类的对象
SuperMan man = new SuperMan();
MyInvocationHandler handler = new MyInvocationHandler();
//返回一个代理类的对象
Object obj = handler.bind(man);
System.out.println(obj.getClass());
Human hu = (Human)obj;
//通过代理类的对象调用重写的抽象方法
hu.info();
System.out.println();
hu.fly();
}
测试结果如下:
// 这里表明拿到的是代理对象
class com.web.test.$Proxy0
我是超人!我怕谁!
I believe I can fly!
【4】JDK动态代理原理
① 首先看下面关键代码
Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
.getClass().getInterfaces(), this);
根据类加载器,目标类的上行接口和InvocationHandler获取代理对象。这里有两个问题,第一如何获取?第二获取的代理对象是个什么样子?
先分析如何获取。
② 追踪源码到Proxy.newProxyInstance
/**
* Returns an instance of a proxy class for the specified interfaces
* that dispatches method invocations to the specified invocation
* handler.
*
* <p>{@code Proxy.newProxyInstance} throws
* {@code IllegalArgumentException} for the same reasons that
* {@code Proxy.getProxyClass} does.
*
* @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
* // h -- 用来转发/反射目标方法的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
* //返回一个代理类的实例对象附带指定的invocation handler。
* //该代理类被指定的类加载器定义且实现了指定的接口。
*/
@CallerSensitive
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
Objects.requireNonNull(h);
//...
/*寻找或者产生代理类的class对象
* Look up or generate the designated proxy class.
*/
Class<?> cl = getProxyClass0(loader, intfs);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (sm != null) {
checkNewProxyPermission(Reflection.getCallerClass(), cl);
}
// 拿到代理类的构造器
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;
}
});
}
// 通过构造器反射获取代理类实例对象(InvocationHandler作为参数)
return cons.newInstance(new Object[]{h});
}
//...
}
过程很清晰(参考代码注释),继续细究下去。
③ Class<?> cl = getProxyClass0(loader, intfs)
做了什么?
源码如下:
/**
* Generate a proxy class. Must call the checkProxyAccess method
* to perform permission checks before calling this.
*/
private static Class<?> getProxyClass0(ClassLoader loader,
Class<?>... interfaces) {
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
return proxyClassCache.get(loader, interfaces);
}
如果缓存中有,就直接返回proxy class;否则就要通过ProxyClassFactory获取。
这里的cache 是WeakCache,暂且不去理会,继续看ProxyClassFactory。
Proxy的静态内部类–Proxy$ProxyClassFactory源码如下:
/**
* A factory function that generates, defines and returns the proxy class given
* the ClassLoader and array of interfaces.
*/
private static final class ProxyClassFactory
implements BiFunction<ClassLoader, Class<?>[], Class<?>>
{
// prefix for all proxy class names--代理类前缀
private static final String proxyClassNamePrefix = "$Proxy";
// next number to use for generation of unique proxy class names
//代理类编号
private static final AtomicLong nextUniqueNumber = new AtomicLong();
@Override
public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
//...省略代码
// package to define proxy class in
//定义代理类所在的包
String proxyPkg = null;
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.
*/
for (Class<?> intf : interfaces) {
int flags = intf.getModifiers();
// 如果修饰符非public,则变为final
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();
// 代理类的完整名字
String proxyName = proxyPkg + proxyClassNamePrefix + num;
/* Generate the specified proxy class.*/
// 这里,产生指定的proxy class 字节数组!!!
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
// 通过一个native方法获取proxy class
return defineClass0(loader, proxyName,
proxyClassFile, 0, proxyClassFile.length);
}
// ... 省略代码
}
}
从ProxyClassFactory中下面的方法可以看到具体生成字节流的方法是ProxyGenerator.generateProxyClass(..).
。最后通过native方法生成Class对象。同时对class对象的包名称有一些规定比如命名为com.web.test$proxy0。
要想得到字节码实例我们需要先下载这部分字节流,然后通过反编译得到java代码。
用ProxyGenerator.generateProxyClass(..)方法生成字节流,然后写进硬盘.假设我把proxyName定义为Human$ProxyCode。
获取代理类代码如下:
public class TestProxyJava {
public static void generateClassFile(Class clazz,String proxyName) throws Exception
{
//根据类信息和提供的代理类名称,生成字节码
byte[] classFile = ProxyGenerator.generateProxyClass(proxyName,new Class[]{clazz});
String paths = clazz.getResource(".").getPath();
System.out.println(paths);
FileOutputStream out = null;
//保留到硬盘中
out = new FileOutputStream(paths+proxyName+".class");
out.write(classFile);
out.flush();
}
public static void main(String[] args) {
try {
generateClassFile(Human.class,"Human$ProxyCode");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
获取到的代理类源码如下:
import com.web.test.Human;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class Human$ProxyCode extends Proxy
implements Human
{
private static Method m1;
private static Method m3;
private static Method m4;
private static Method m2;
private static Method m0;
public Human$ProxyCode(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 (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final void fly()
throws
{
try
{
this.h.invoke(this, m3, null);
return;
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final void info()
throws
{
try
{
this.h.invoke(this, m4, null);
return;
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final String toString()
throws
{
try
{
return (String)this.h.invoke(this, m2, null);
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final int hashCode()
throws
{
try
{
return ((Integer)this.h.invoke(this, m0, null)).intValue();
}
catch (Error|RuntimeException localError)
{
throw localError;
}
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("com.web.test.Human").getMethod("fly", new Class[0]);
m4 = Class.forName("com.web.test.Human").getMethod("info", 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());
}
}
}
可以发现这个类实现了我们需要代理的接口Human,且他的构造函数确实是需要传递一个InvocationHandler对象。
那么现在的情况就是我们的生成了一个代理类,这个代理类是我们需要代理的接口的实现类。我们的接口中定义的info和fly方法,在这个代理类中帮我们实现了,并且全部变成了final的。同时覆盖了一些Object类中的方法。
以info这个方法举例,方法中会调用InvocationHandler类中的invoke方法(也就是我们实现的逻辑的地方),同时把自己的Method对象,参数列表等传入进去。
再回头看上面的测试main方法:
public static void main(String[] args) {
SuperMan man = new SuperMan();//创建一个被代理类的对象
MyInvocationHandler handler = new MyInvocationHandler();
Object obj = handler.bind(man);//返回一个代理类的对象
System.out.println(obj.getClass());
// 现在知道为什么可以强转了吧
Human hu = (Human)obj;
//通过代理类的对象调用重写的抽象方法
hu.info();
System.out.println();
hu.fly();
}
【5】动态代理与AOP
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要功能:日志记录,性能统计,安全控制,事务处理,异常处理等等。
简而言之,在不改变原有代码基础上进行功能增强!
添加HumanUtil模拟增强方法:
class HumanUtil {
public void method1() {
System.out.println("=======方法一=======");
}
public void method2() {
System.out.println("=======方法二=======");
}
}
修改MyInvocationHandler如下:
class MyInvocationHandler implements InvocationHandler {
// 被代理类对象的声明
Object obj;
// 动态的创建一个代理类的对象
public Object bind(Object obj){
this.obj = obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
HumanUtil h = new HumanUtil();
h.method1();
Object returnVal = method.invoke(obj, args);
h.method2();
return returnVal;
}
}
再次测试如下:
class com.web.test.$Proxy0
=======方法一=======
我是超人!我怕谁!
=======方法二=======
=======方法一=======
I believe I can fly!
=======方法二=======
使用Proxy生成一个动态代理时,往往并不会凭空产生一个动态代理,这样没有太大的意义。通常都是为指定的目标对象生成动态代理。
这种动态代理在AOP中被称为AOP代理,AOP代理可代替目标对象,AOP代理包含了目标对象的全部方法。
但AOP代理中的方法与目标对象的方法存在差异:AOP代理里的方法可以在执行目标方法之前、之后插入一些通用处理。
如下图所示:
【6】JDK动态代理总结
① JDK动态代理只能代理有接口的类,并且是能代理接口方法,不能代理一般的类中自定义的方法
因为代理类是一个实现了被代理对象的上行接口的类,所以类必须有接口。而且不能代理被代理对象中自定义的方法!
② 提供了一个使用InvocationHandler作为参数的构造方法
在代理类中做一层包装,业务逻辑在invoke方法中实现。
③ 重写了Object类的equals、hashCode、toString
它们都只是简单的调用了InvocationHandler的invoke方法,即可以对其进行特殊的操作,也就是说JDK的动态代理还可以代理上述三个方法。
④ 在invoke方法中我们甚至可以不用Method.invoke方法调用实现类就返回
这种方式常常用在RPC框架中,在invoke方法中发起通信调用远端的接口等
【7】获取代理类class文件的简单方法
在【4】中通过ProxyGenerator.generateProxyClass(proxyName,new Class[]{clazz});
获取class文件,比较繁琐,这里介绍一种简单方式。
关键代码如下:
//生成$Proxy0的class文件
System.getProperties().
put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
修改main方法如下:
public static void main(String[] args) {
//生成$Proxy0的class文件
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
SuperMan man = new SuperMan();//创建一个被代理类的对象
MyInvocationHandler handler = new MyInvocationHandler();
Object obj = handler.bind(man);//返回一个代理类的对象
System.out.println(obj.getClass());
Human hu = (Human)obj;
hu.info();//通过代理类的对象调用重写的抽象方法
System.out.println();
hu.fly();
}
生成的$Proxy0
路径在项目根目录下com/web/test
–该包名为测试代码所在的包。
关于Cglib动态代理与Spring中动态代理思想参考博客:
https://blog.csdn.net/J080624/article/details/82079072
其他博客参考: