什么是代理模式?
如果用专业术语来解:为其他对象提供一种代理以控制对这个对象的访问。如果投影在生活中,它可以理解成中介 黄牛 经纪人等…
解决的问题:
在直接访问对象时带来的问题,比如说:要访问的对象在远程的机器上。在面向对象系统中,有些对象由于某些原因(比如对象创建开销很大,或者某些操作需要安全控制,或者需要进程外的访问),直接访问会给使用者或者系统结构带来很多麻烦,我们可以在访问此对象时加上一个对此对象的访问层。说白了就是在你代码前面插一段后面插一段。
Java动态代理实现方式:
- JDK 自带的动态代理
- Cglib动态代理
1. JDK 自带的动态代理
我以黄牛为例,黄牛刚开始了解该人需求,该人将信息(JAY演唱会门票)给予黄牛,黄牛给票。黄牛就是该买票人的代理。
1.1 People.java
注意这必须是一个接口,原因往下看。
-
public interface People {
-
/**
-
* 交谈
-
*/
-
void speak();
-
}
这个接口很简单,就是一个讲话的功能,但是它为什么必须是一个接口呢。因为在HuangNiu这个类中,Proxy.newProxyInstance 这个方法的实现需要接口,这一点我在HuangNiu类下解释的很清楚,往下看。
1.2 HuangNiu.java
黄牛代理类,获取到People信息后调用Proxy来生成一个新的代理类,它必须实现InvocationHandler接口,这个接口使得它可以通过invoke方法实现对真实角色(People)的代理访问。
-
public class HuangNiu implements InvocationHandler {
-
private People people;
-
/**
-
* 获取被代理对象信息
-
*/
-
public Object getInstance(People people){
-
this.people = people;
-
Class clazz = people.getClass();
-
System.out.println("没生成代理之前的class对象:"+clazz );
-
return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
-
}
-
@Override
-
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
-
System.out.println("代理中...");
-
method.invoke(people);
-
System.out.println("代理处理完毕,OK,请查收");
-
return null;
-
}
-
}
在实例化HuangNiu这个对象的时候,我们调用了Proxy的newProxyInstance方法:
-
return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
其中clazz是People的class对象。再来看看newProxyInstance的源码实现:
-
public static Object newProxyInstance(ClassLoader loader,
-
Class<?>[] interfaces,
-
InvocationHandler h){
-
final Class<?>[] intfs = interfaces.clone();
-
//Look up or generate the designated proxy class.
-
Class<?> cl = getProxyClass0(loader, intfs);
-
//获取代理类的构造函数对象
-
//参数constructorParames为常量值:private static final Class<?>[] constructorParams = { InvocationHandler.class };
-
final Constructor<?> cons = cl.getConstructor(constructorParames);
-
final InvocationHandler ih = h;
-
//根据代理类的构造函数对象来创建代理类对象
-
return newInstance(cons, ih);
-
}
getProxyClass0方法源码:
-
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);
-
}
如果缓存中有该代理类,则取缓存,如果没有,则通过ProxyClassFactory来创建代理类。如果对如何生成代理类感兴趣则追踪下去即可。
我只取了核心代码和注释,可以看到JDK的动态代理实现是依据接口来重新生成一个新的代理类,
什么是新的代理类?
通俗点说就是综合和前后代理逻辑并重新生成一份.class文件来实现动态代理的类,下面也会具体说。
1.3 Me.java
被代理对象,实现了People接口,给代理提供需要的信息来实现被代理。
-
public class Me implements People {
-
private String name;
-
private String type;
-
Me(String name, String type){
-
this.name = name;
-
this.type = type;
-
}
-
@Override
-
public void speak() {
-
System.out.println("我叫"+name+", 我要一张"+type);
-
}
-
}
1.4 Main.java
-
public class Main {
-
public static void main(String[] args) {
-
People instance = (People)new HuangNiu().getInstance(new Me("Fantj", "JAY演唱会门票"));
-
instance.speak();
-
System.out.println("生成代理对象后对象变成:"+instance.getClass());
-
}
-
}
执行结果:
-
没生成代理之前的class对象:class com.fantj.proxy.jdk.Me
-
代理中...
-
我叫Fantj, 我要一张JAY演唱会门票
-
代理处理完毕,OK,请查收
-
生成代理对象后对象变成:class com.sun.proxy.$Proxy0
为了证明事实上真的有代理类的产生,我在代理完成前和代理完成后分别打印出它的类信息,可以看出是不同的,可以猜想到代理中是有代理类产生的,这个代理类就是$Proxy0。
那既然知道了这个类的信息,我们就可以逆向生成这个.class文件来看看(在main方法后追加):
-
/**
-
* 生成代码
-
*/
-
try {
-
byte[] $Proxy0s = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{instance.getClass()});
-
String path = Main.class.getResource("").toString();
-
// System.out.println("get the path"+path);
-
FileOutputStream fileOutputStream = new FileOutputStream("$Proxy0.class");
-
fileOutputStream.write($Proxy0s);
-
fileOutputStream.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
它默认生成在项目根目录下: 我使用的IDEA工具会自动反编译.class文件为java代码,直接打开即刻看到源码,如果用别的工具的可以下载反编译工具来进行反编译。
$Proxy0.class
-
//
-
// Source code recreated from a .class file by IntelliJ IDEA
-
// (powered by Fernflower decompiler)
-
//
-
import com.sun.proxy..Proxy0;
-
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 Proxy0 {
-
private static Method m1;
-
private static Method m5;
-
private static Method m2;
-
private static Method m4;
-
private static Method m11;
-
private static Method m13;
-
private static Method m0;
-
private static Method m10;
-
private static Method m12;
-
private static Method m6;
-
private static Method m9;
-
private static Method m3;
-
private static Method m7;
-
private static Method m8;
-
public $Proxy0(InvocationHandler var1) throws {
-
super(var1);
-
}
-
public final boolean equals(Object var1) throws {
-
try {
-
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
-
} catch (RuntimeException | Error var3) {
-
throw var3;
-
} catch (Throwable var4) {
-
throw new UndeclaredThrowableException(var4);
-
}
-
}
-
public final InvocationHandler getInvocationHandler(Object var1) throws IllegalArgumentException {
-
try {
-
return (InvocationHandler)super.h.invoke(this, m5, new Object[]{var1});
-
} catch (RuntimeException | Error var3) {
-
throw var3;
-
} catch (Throwable var4) {
-
throw new UndeclaredThrowableException(var4);
-
}
-
}
-
public final String toString() throws {
-
try {
-
return (String)super.h.invoke(this, m2, (Object[])null);
-
} catch (RuntimeException | Error var2) {
-
throw var2;
-
} catch (Throwable var3) {
-
throw new UndeclaredThrowableException(var3);
-
}
-
}
-
public final Class getProxyClass(ClassLoader var1, Class[] var2) throws IllegalArgumentException {
-
try {
-
return (Class)super.h.invoke(this, m4, new Object[]{var1, var2});
-
} catch (RuntimeException | Error var4) {
-
throw var4;
-
} catch (Throwable var5) {
-
throw new UndeclaredThrowableException(var5);
-
}
-
}
-
public final Class getClass() throws {
-
try {
-
return (Class)super.h.invoke(this, m11, (Object[])null);
-
} catch (RuntimeException | Error var2) {
-
throw var2;
-
} catch (Throwable var3) {
-
throw new UndeclaredThrowableException(var3);
-
}
-
}
-
public final void notifyAll() throws {
-
try {
-
super.h.invoke(this, m13, (Object[])null);
-
} catch (RuntimeException | Error var2) {
-
throw var2;
-
} catch (Throwable var3) {
-
throw new UndeclaredThrowableException(var3);
-
}
-
}
-
public final int hashCode() throws {
-
try {
-
return (Integer)super.h.invoke(this, m0, (Object[])null);
-
} catch (RuntimeException | Error var2) {
-
throw var2;
-
} catch (Throwable var3) {
-
throw new UndeclaredThrowableException(var3);
-
}
-
}
-
public final void wait() throws InterruptedException {
-
try {
-
super.h.invoke(this, m10, (Object[])null);
-
} catch (RuntimeException | InterruptedException | Error var2) {
-
throw var2;
-
} catch (Throwable var3) {
-
throw new UndeclaredThrowableException(var3);
-
}
-
}
-
public final void notify() throws {
-
try {
-
super.h.invoke(this, m12, (Object[])null);
-
} catch (RuntimeException | Error var2) {
-
throw var2;
-
} catch (Throwable var3) {
-
throw new UndeclaredThrowableException(var3);
-
}
-
}
-
public final Object newProxyInstance(ClassLoader var1, Class[] var2, InvocationHandler var3) throws IllegalArgumentException {
-
try {
-
return (Object)super.h.invoke(this, m6, new Object[]{var1, var2, var3});
-
} catch (RuntimeException | Error var5) {
-
throw var5;
-
} catch (Throwable var6) {
-
throw new UndeclaredThrowableException(var6);
-
}
-
}
-
public final void wait(long var1) throws InterruptedException {
-
try {
-
super.h.invoke(this, m9, new Object[]{var1});
-
} catch (RuntimeException | InterruptedException | Error var4) {
-
throw var4;
-
} catch (Throwable var5) {
-
throw new UndeclaredThrowableException(var5);
-
}
-
}
-
public final void speak() throws {
-
try {
-
super.h.invoke(this, m3, (Object[])null);
-
} catch (RuntimeException | Error var2) {
-
throw var2;
-
} catch (Throwable var3) {
-
throw new UndeclaredThrowableException(var3);
-
}
-
}
-
public final boolean isProxyClass(Class var1) throws {
-
try {
-
return (Boolean)super.h.invoke(this, m7, new Object[]{var1});
-
} catch (RuntimeException | Error var3) {
-
throw var3;
-
} catch (Throwable var4) {
-
throw new UndeclaredThrowableException(var4);
-
}
-
}
-
public final void wait(long var1, int var3) throws InterruptedException {
-
try {
-
super.h.invoke(this, m8, new Object[]{var1, var3});
-
} catch (RuntimeException | InterruptedException | Error var5) {
-
throw var5;
-
} catch (Throwable var6) {
-
throw new UndeclaredThrowableException(var6);
-
}
-
}
-
static {
-
try {
-
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
-
m5 = Class.forName("com.sun.proxy.$Proxy0").getMethod("getInvocationHandler", Class.forName("java.lang.Object"));
-
m2 = Class.forName("java.lang.Object").getMethod("toString");
-
m4 = Class.forName("com.sun.proxy.$Proxy0").getMethod("getProxyClass", Class.forName("java.lang.ClassLoader"), Class.forName("[Ljava.lang.Class;"));
-
m11 = Class.forName("com.sun.proxy.$Proxy0").getMethod("getClass");
-
m13 = Class.forName("com.sun.proxy.$Proxy0").getMethod("notifyAll");
-
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
-
m10 = Class.forName("com.sun.proxy.$Proxy0").getMethod("wait");
-
m12 = Class.forName("com.sun.proxy.$Proxy0").getMethod("notify");
-
m6 = Class.forName("com.sun.proxy.$Proxy0").getMethod("newProxyInstance", Class.forName("java.lang.ClassLoader"), Class.forName("[Ljava.lang.Class;"), Class.forName("java.lang.reflect.InvocationHandler"));
-
m9 = Class.forName("com.sun.proxy.$Proxy0").getMethod("wait", Long.TYPE);
-
m3 = Class.forName("com.sun.proxy.$Proxy0").getMethod("speak");
-
m7 = Class.forName("com.sun.proxy.$Proxy0").getMethod("isProxyClass", Class.forName("java.lang.Class"));
-
m8 = Class.forName("com.sun.proxy.$Proxy0").getMethod("wait", Long.TYPE, Integer.TYPE);
-
} catch (NoSuchMethodException var2) {
-
throw new NoSuchMethodError(var2.getMessage());
-
} catch (ClassNotFoundException var3) {
-
throw new NoClassDefFoundError(var3.getMessage());
-
}
-
}
-
}
里面大多是Object中的方法,还有我们定义的speak()方法,然后就是静态代码块(对变量进行初始化)。