1.代理模式
代理模式 是常用的设计模式之一, 其特征是代理类与被代理类有相同的接口,代理类可以为被代理类方法执行进行前置后置处理,增强被代理类方法
2. 动态代理使用
代理类并不是在 Java 代码中定义,而是在运行时根据在 Java 代码中的“指示”动态生成( 字节码 由 JVM 在运行时动态生成而非预存在任何一个 .class 文件中), 这种在程序运行时创建代理类的代理方式被称为动态代理,它的优势在于可以方便地对代理类的函数进行统一处理。
这是因为所有被代理执行的方法,都是通过 InvocationHandler#invoke() 方法调用,相当于给被代理类所有方法套了一层壳,所以只要在这个方法中统一处理,就可以对所有被代理的方法进行相同的操作了
以下代码展示了动态代理的简单使用,其基本步骤如下:
- 定义为一个公共接口,本例中为 IHello ,接口中有一个抽象方法
- 定义一个实现了公共接口的实体类作为被代理类,本例中被代理类 Hello 实现了 IHello 接口,重写了接口中的抽象方法
- 定义一个实现了 Invocation handler 接口的方法拦截类,重写 invoke() 方法实现拦截到被代理类方法执行时候的处理逻辑
- 通过 Proxy.newProxyInstance() 方法生成代理对象,持有代理对象之后执行接口方法即可
public class ServiceProxy {
public interface IHello {
String sayHi();
}
public static class Hello implements IHello {
@Override
public String sayHi() {
return "Hello";
}
}
// 动态代理类
public static class ProxyHandler<T> implements InvocationHandler {
private T origin;
public ProxyHandler(T origin) {
this.origin = origin;
}
/**
* @param o 代理对象引用
* @param method 正在执行目标的方法
* @param objects 目标方法执行时的入参
*/ @Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
String s = "proxy";
s += method.invoke(origin, objects);
return s;
}
}
public static void main(String[] args) {
IHello IHello = (IHello) getInstance (IHello.class, new ProxyHandler<>(new Hello()));
System.out.println(IHello.toString());
generateProxyClass();
}
// 创建代理对象
public static <T> Object getInstance(Class<T> clazz, ProxyHandler<T> handler) {
return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, handler);
}
private static Void generateProxyClass() {
byte [] class File = ProxyGenerator.generateProxyClass("$Proxy0", Hello.class.getInterfaces());
String path = "/Users/nathan.yang/workspace/algorithm_Java/out/StuProxy.class";
try (FileOutputStream fos = new FileOutputStream(path)) {
fos.write(classFile);
fos.flush();
System.out.println("代理类文件写入成功");
} catch ( Exception e) {
System.out.println("写文件错误");
}
}
}
3. 动态代理原理
1. Proxy#newProxyInstance() 方法是动态代理的入口,其生成动态代理对象主要有以下几个步骤:
- getProxyClass0() 方法生成代理类
- 获取到代理类后将 InvocationHandler 对象入参,反射调用 构造方法 生成动态代理对象
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
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.
*/ Class<?> cl = getProxyClass(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;
}
});
}
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);
}
}
2. Proxy#getProxyClass0() 这个方法其实是从一个 WeakCache 中去获取代理类,其获取逻辑是如果缓存类中没有代理类的话就调用 ProxyClassFactory#apply() ,通过代理类工厂去即时生成一个代理类,其步骤如下:
- 首先通过指定的类加载器去验证目标接口是否可以被其加载
- 通过接口所在包等条件决定代理类所在包及代理类的全限定名称,代理类名称是 包名+$Proxy+id
- 通过 ProxyGenerator.generateProxyClass() 生成字节码数组,然后调用 native 方法 defineClass0() 将其动态生成的代理类字节码加载到内存中
private static Class<?> getProxyClass(ClassLoader loader,
Class<?>... interfaces) {
if (interfaces.length >) {
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);
}
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.
*/ 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 == -) ? "" : 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.
*/ byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
return defineClass(loader, proxyName,
proxyClassFile,, 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());
}
}
3.反射获取到的代理类参数为 InvocationHandler.class 的构造器,其实也就是 Proxy 的带参构造器,调用构造器 cons.newInstance(new Object[]{h}) 生成代理对象
protected Proxy(InvocationHandler h) {
Objects.requireNonNull(h);
this.h = h;
}
4.通过以下代码可以将 JVM 中加载的代理类输出成 class 文件,之后就可以使用反编译工具查看代理类的源码
private static void generateProxyClass() {
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy", Hello.class.getInterfaces());
String path = "/Users/nathan/workspace/algorithm_Java/out/StuProxy.class";
try (FileOutputStream fos = new FileOutputStream(path)) {
fos.write(classFile);
fos.flush();
System.out.println("代理类文件写入成功");
} catch (Exception e) {
System.out.println("写文件错误");
}
}
5.生成的代理类源码如下,很明显可以看到该类实现动态代理的原理:
- 通过 static 代码块将被代理类中每一个方法封装为 Method 对象,生成方法表
- 代理类对象执行被代理类同名方法时,通过其父类 Proxy 保留的指向 InvocationHandler 对象的引用调用 InvocationHandler#invoke() 方法,完成动态代理
public final class $Proxy extends Proxy implements IHello {
private static Method m;
private static Method m;
private static Method m;
private static Method m;
public $Proxy(InvocationHandler var1) throws {
super(var);
}
public final String sayHi() throws {
try {
// 父类 Proxy 保留的指向 InvocationHandler 对象的引用调用 invoke() 方法
return (String)super.h.invoke(this, m, (Object[])null);
} catch (RuntimeException | Error var) {
throw var;
} catch (Throwable var) {
throw new UndeclaredThrowableException(var);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m, (Object[])null);
} catch (RuntimeException | Error var) {
throw var;
} catch (Throwable var) {
throw new UndeclaredThrowableException(var);
}
}
......
// 方法表
static {
try {
m = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m = Class.forName("ServiceProxy$IHello").getMethod("sayHi");
m = Class.forName("java.lang.Object").getMethod("toString");
m = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var) {
throw new NoSuchMethodError(var.getMessage());
} catch (ClassNotFoundException var) {
throw new NoClassDefFoundError(var.getMessage());
}
}
}