代理模式是软件开发中常见的设计模式,它的目的是让调用者不用持有具体操做者的引用,而是经过代理者去对具体操做者执行具体的操做。java
操做接口:git
public interface Operate {
void doSomething();
}
复制代码
操做者:github
public class Operator implements Operate {
@Override
public void doSomething() {
System.out.println("I'm doing something");
}
}
复制代码
代理者:设计模式
public class OperationProxy implements Operate {
private Operator operator = null;
@Override
public void doSomething() {
beforeDoSomething();
if(operator == null){
operator = new Operator();
}
operator.doSomething();
afterDoSomething();
}
private void beforeDoSomething() {
System.out.println("before doing something");
}
private void afterDoSomething() {
System.out.println("after doing something");
}
}
复制代码
调用者:数组
public class StaticProxyTest {
public static void main(String[] args) {
Operate operate = new OperationProxy();//使用OperationProxy代替Operator
operate.doSomething(); //代理者代替真实者作事情
}
}
复制代码
能够看到,静态代理让调用者不用再直接持有操做者的引用,而是将一切操做交由代理者去完成。可是静态代理也有它的局限性:bash
可能有人想到能够用策略模式和工厂模式分别解决上面两个问题,可是,有没有更加巧妙的方法呢?首先,咱们了解一下 Java 代码的执行过程。ide
要从根本上理解动态代理的实现原理,得先从 Java 代码的执行流程提及:工具
JVM 在运行 .class 文件以前,首先经过 ClassLoader 将 .class 文件以二进制的形式解析并生成实例以供调用,咱们的代码执行逻辑是在 JVM 的运行期系统中进行工做的,那么,咱们可不能够在本身的代码里面按照 .class 的格式生成本身的 .class 文件,进而调用自定义的 ClassLoader 将其加载出来呢?答案是确定的,这样咱们就能够动态地建立一个类了。ui
固然咱们不用手动去一点一点拼装 .class 文件,目前比较经常使用的字节码生成工具备 ASM 和 Javassist,根据这个思路,生成 .class 文件的过程以下:this
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
public class Test {
public static void main(String[] args) throws Exception {
ClassPool pool = ClassPool.getDefault();
//建立 AutoGenerateClass 类
CtClass cc= pool.makeClass("com.guanpj.AutoGenerateClass");
//定义 show 方法
CtMethod method = CtNewMethod.make("public void show(){}", cc);
//插入方法代码
method.insertBefore("System.out.println(\"I'm just test generate .class file by javassit.....\");");
cc.addMethod(method);
//保存生成的字节码
cc.writeFile("D://temp");
}
}
复制代码
生成的 .class 文件以下:
反编译后查看内容:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.guanpj;
public class AutoGenerateClass {
public void show() {
System.out.println("I'm just test generate .class file by javassit.....");
}
public AutoGenerateClass() {
}
}
复制代码
能够看到,javassit 生成的类中,除了 show() 方法以外还默认生成了一个无参的构造方法。
为了可以让自定的类被加载出来,咱们自定义了一个类加载器来加载指定的 .class 文件:
public class CustomClassLoader extends ClassLoader {
public CustomClassLoader() {
}
protected Class<?> findClass(String className) {
String path = "D://temp//" + className.replace(".","//") + ".class";
byte[] classData = getClassData(path);
return defineClass(className, classData, 0, classData.length);
}
private byte[] getClassData(String path) {
try {
InputStream ins = new FileInputStream(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int bytesNumRead = 0;
while ((bytesNumRead = ins.read(buffer)) != -1) {
baos.write(buffer, 0, bytesNumRead);
}
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
复制代码
接着,用 ClassLoader 加载刚才生成的 .class 文件:
public class TestLoadClass {
public static void main(String[] args) throws Exception {
CustomClassLoader classLoader = new CustomClassLoader();
Class clazz = classLoader.findClass("com.guanpj.AutoGenerateClass");
Object object = clazz.newInstance();
Method showMethod = clazz.getMethod("show", null);
showMethod.invoke(object, null);
}
}
复制代码
后台输出以下:
成功执行了 show 方法!
使用动态代理的初衷是简化代码,不论是 ASM 仍是 Javassist,在进行动态代理的时候操做仍是不够简便,这也违背了咱们的初衷。咱们来看一下怎么 InvocationHandler 怎么作:
InvocationHandler:
public class InvocationHandlerImpl implements InvocationHandler {
Operate operate;
//注入操做者对象
public InvocationHandlerImpl(Operate operate) {
this.operate = operate;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before calling method: " + method.getName());
//调用操纵者的具体操做方法
method.invoke(operate, args);
System.out.println("after calling method: " + method.getName());
return null;
}
}
复制代码
调用者:
public class DynamicProxyTest {
public static void main(String[] args) {
//实例化操做者
Operate operate = new Operator();
//将操做者对象进行注入
InvocationHandlerImpl handler = new InvocationHandlerImpl(operate);
//生成代理对象
Operate operationProxy = (Operate) Proxy.newProxyInstance(operate.getClass().getClassLoader(),
operate.getClass().getInterfaces(), handler);
//调用操做方法
operationProxy.doSomething();
}
}
复制代码
跟静态代理不一样的是,动态代理的过程主要分为三个步骤
用 Proxy 类生成代理类的方法为 newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) ,第二个参数是操做者的接口数组,意味着只能代理它实现的接口里的方法,对于原本在操做者类中定义的方法表示无能为力,CGLIB(Code Generation Library) 解决了这个问题。
MethodInterceptorImpl:
public class MethodInterceptorImpl implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("before calling method:" + method.getName());
proxy.invokeSuper(obj, args);
System.out.println("after calling method:" + method.getName());
return null;
}
}
复制代码
调用者:
public class ProxyTest {
public static void main(String[] args) {
Operator operator = new Operator();
MethodInterceptorImpl methodInterceptorImpl = new MethodInterceptorImpl();
//初始化增强器对象
Enhancer enhancer = new Enhancer();
//设置代理类
enhancer.setSuperclass(operator.getClass());
//设置代理回调
enhancer.setCallback(methodInterceptorImpl);
//建立代理对象
Operator operationProxy = (Operator) enhancer.create();
//调用操做方法
operationProxy.doSomething();
}
}
复制代码
使用 CGLIB 进行动态代理的过程分为四个步骤:
不管是静态代理仍是动态代理,都能必定程度地解决咱们的问题,在开发过程当中能够根据实际状况选择合适的方案。总之,没有好很差的方案,只有适不适合本身项目的方案,咱们应该深刻研究和理解方案背后的原理,以便可以应对开发过程当中产生的变数。
文章中的代码已经上传至个人 Github,若是你对文章内容有不一样意见,欢迎留言,咱们一同探讨。