Java中的Cglib动态代理详细解读
作者:后仰大风车
前言
摘自OSCHINA(开源中国)的介绍:
CGLib (Code Generation Library) 是一个强大的、高性能、高质量的 Code 生成类库。它可以在运行期扩展 Java 类与实现 Java 接口。Hibernate 用它来实现 PO 字节码的动态生成。
CGLib 比 Java 的 java.lang.reflect.Proxy 类更强的在于它不仅可以接管接口类的方法,还可以接管普通类的方法。 CGLib 的底层是 Java 字节码操作框架 —— ASM。
Cglib动态代理的简单示例
1、引入Cglib的maven依赖:
<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.3.0</version> </dependency>
2、定义一个目标类,也就是被代理类,此时是一个图书管理员类,管理一些Book类对象(Book类为简单实体类,此处不贴代码)。
package org.example.proxy.cglib; import org.example.domain.Book; import java.util.HashMap; import java.util.Map; public class Librarian { private Map<String, Book> books = new HashMap<>(); public void addBook(Book book) { String name = book.getBookName(); if (!books.containsKey(name)) { books.put(name, book); } } public Book getBook(String name) { System.out.println("这里是 " + this.getClass().getName() + " 的getBook方法"); return books.getOrDefault(name, null); } }
3、定义一个代理工厂类,用来创建代理对象:
package org.example.proxy.cglib; import net.sf.cglib.core.DebuggingClassWriter; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import org.example.domain.Book; import java.lang.reflect.Method; public class CglibProxyFactory { public Object getProxy(Object targetObject) { Enhancer enhancer = new Enhancer(); //Cglib代理基于创建子类重写父类方法实现,所以这里要确定父类,也就是被代理类。 Class<?> superClass = targetObject.getClass(); enhancer.setSuperclass(superClass); /* 创建了一个MethodInterceptor拦截器接口的实现类对象,重写intercept回调方法, 参数依次为:代理对象、代理方法、方法参数、方法代理 */ MethodInterceptor interceptor = new MethodInterceptor() { @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("这是前置增强"); Object res = methodProxy.invokeSuper(o, objects); System.out.println("这是后置增强"); return res; } }; enhancer.setCallback(interceptor); return enhancer.create(); } public static void main(String[] args) { //这里设置一个系统属性,保存Cglib动态代理类的字节码文件 System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "target/classes"); //创建原始对象 Librarian librarian = new Librarian("LiLei", 18); Book book = new Book(); book.setBookName("钢铁是怎样炼成的"); librarian.addBook(book); librarian.getBook("111"); //创建代理对象 CglibProxyFactory cglibProxyFactory = new CglibProxyFactory(); Librarian librarianProxy = (Librarian) cglibProxyFactory.getProxy(librarian); librarianProxy.getBook("111"); } }
运行结果如下:
原理
沿着enhancer.create()方法一直往下debug,会走到Enhancer类的generate(ClassLoaderData data)方法中,该方法返回的是代理类的Class 对象。
protected Object create(Object key) { try { ClassLoader loader = getClassLoader(); Map<ClassLoader, ClassLoaderData> cache = CACHE; ClassLoaderData data = cache.get(loader); if (data == null) { synchronized (AbstractClassGenerator.class) { cache = CACHE; data = cache.get(loader); if (data == null) { Map<ClassLoader, ClassLoaderData> newCache = new WeakHashMap<ClassLoader, ClassLoaderData>(cache); data = new ClassLoaderData(loader); newCache.put(loader, data); CACHE = newCache; } } } this.key = key; //这里拿到一个Enhancer$EnhancerFactoryData对象,包括代理类Class对象、构造器等信息。 Object obj = data.get(this, getUseCache()); if (obj instanceof Class) { return firstInstance((Class) obj); } //使用拿到的Enhancer$EnhancerFactoryData对象,实例化代理类对象。 return nextInstance(obj); } catch (RuntimeException e) { throw e; } catch (Error e) { throw e; } catch (Exception e) { throw new CodeGenerationException(e); } }
1)沿着data.get(this, getUseCache())方法往下走,一直到Enhancer父类的generate(ClassLoaderData data)方法中,就是该方法生成了代理类的Class 对象。
protected Class generate(ClassLoaderData data) { Class gen; Object save = CURRENT.get(); CURRENT.set(this); try { ClassLoader classLoader = data.getClassLoader(); if (classLoader == null) { throw new IllegalStateException("ClassLoader is null while trying to define class " + getClassName() + ". It seems that the loader has been expired from a weak reference somehow. " + "Please file an issue at cglib's issue tracker."); } synchronized (classLoader) { String name = generateClassName(data.getUniqueNamePredicate()); data.reserveName(name); this.setClassName(name); } if (attemptLoad) { try { gen = classLoader.loadClass(getClassName()); return gen; } catch (ClassNotFoundException e) { // ignore } } //结合当前Enhancer及父类信息生成代理类字节码 byte[] b = strategy.generate(this); String className = ClassNameReader.getClassName(new ClassReader(b)); ProtectionDomain protectionDomain = getProtectionDomain(); synchronized (classLoader) { // just in case if (protectionDomain == null) { gen = ReflectUtils.defineClass(className, b, classLoader); } else { /* 内部调用了ClassLoader类的defineClass方法,将字节码转化成类的Class实例,然后调用Class.forName(初始化参数设置为 true)强制初始化。 */ gen = ReflectUtils.defineClass(className, b, classLoader, protectionDomain); } } //返回代理类Class对象 return gen; } catch (RuntimeException e) { throw e; } catch (Error e) { throw e; } catch (Exception e) { throw new CodeGenerationException(e); } finally { CURRENT.set(save); } }
包装后得到的Enhancer$EnhancerFactoryData对象结构:
2)再看nextInstance(obj)方法,这里最终使用构造器的newInstance方法实例化了代理对象。
public Object newInstance(Class[] argumentTypes, Object[] arguments, Callback[] callbacks) { setThreadCallbacks(callbacks); try { // Explicit reference equality is added here just in case Arrays.equals does not have one if (primaryConstructorArgTypes == argumentTypes || Arrays.equals(primaryConstructorArgTypes, argumentTypes)) { // If we have relevant Constructor instance at hand, just call it // This skips "get constructors" machinery //使用构造器的newInstance方法实例化代理对象。 return ReflectUtils.newInstance(primaryConstructor, arguments); } // Take a slow path if observing unexpected argument types return ReflectUtils.newInstance(generatedClass, argumentTypes, arguments); } finally { // clear thread callbacks to allow them to be gc'd setThreadCallbacks(null); } }
最后看看代理类的字节码文件:
Librarian EnhancerByCGLIB 9192983字节码反编译后的代码如下:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package org.example.proxy.cglib; import java.lang.reflect.Method; import net.sf.cglib.core.ReflectUtils; import net.sf.cglib.core.Signature; import net.sf.cglib.proxy.Callback; import net.sf.cglib.proxy.Factory; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import org.example.domain.Book; public class Librarian$$EnhancerByCGLIB$$e9192983 extends Librarian implements Factory { private boolean CGLIB$BOUND; public static Object CGLIB$FACTORY_DATA; private static final ThreadLocal CGLIB$THREAD_CALLBACKS; private static final Callback[] CGLIB$STATIC_CALLBACKS; private MethodInterceptor CGLIB$CALLBACK_0; private static Object CGLIB$CALLBACK_FILTER; private static final Method CGLIB$addBook$0$Method; private static final MethodProxy CGLIB$addBook$0$Proxy; private static final Object[] CGLIB$emptyArgs; private static final Method CGLIB$getBook$1$Method; private static final MethodProxy CGLIB$getBook$1$Proxy; private static final Method CGLIB$equals$2$Method; private static final MethodProxy CGLIB$equals$2$Proxy; private static final Method CGLIB$toString$3$Method; private static final MethodProxy CGLIB$toString$3$Proxy; private static final Method CGLIB$hashCode$4$Method; private static final MethodProxy CGLIB$hashCode$4$Proxy; private static final Method CGLIB$clone$5$Method; private static final MethodProxy CGLIB$clone$5$Proxy; static void CGLIB$STATICHOOK1() { CGLIB$THREAD_CALLBACKS = new ThreadLocal(); CGLIB$emptyArgs = new Object[0]; Class var0 = Class.forName("org.example.proxy.cglib.Librarian$$EnhancerByCGLIB$$e9192983"); Class var1; Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods()); CGLIB$equals$2$Method = var10000[0]; CGLIB$equals$2$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2"); CGLIB$toString$3$Method = var10000[1]; CGLIB$toString$3$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$3"); CGLIB$hashCode$4$Method = var10000[2]; CGLIB$hashCode$4$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$4"); CGLIB$clone$5$Method = var10000[3]; CGLIB$clone$5$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5"); var10000 = ReflectUtils.findMethods(new String[]{"addBook", "(Lorg/example/domain/Book;)V", "getBook", "(Ljava/lang/String;)Lorg/example/domain/Book;"}, (var1 = Class.forName("org.example.proxy.cglib.Librarian")).getDeclaredMethods()); CGLIB$addBook$0$Method = var10000[0]; CGLIB$addBook$0$Proxy = MethodProxy.create(var1, var0, "(Lorg/example/domain/Book;)V", "addBook", "CGLIB$addBook$0"); CGLIB$getBook$1$Method = var10000[1]; CGLIB$getBook$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/String;)Lorg/example/domain/Book;", "getBook", "CGLIB$getBook$1"); } final void CGLIB$addBook$0(Book var1) { super.addBook(var1); } public final void addBook(Book var1) { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { var10000.intercept(this, CGLIB$addBook$0$Method, new Object[]{var1}, CGLIB$addBook$0$Proxy); } else { super.addBook(var1); } } final Book CGLIB$getBook$1(String var1) { return super.getBook(var1); } public final Book getBook(String var1) { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } return var10000 != null ? (Book)var10000.intercept(this, CGLIB$getBook$1$Method, new Object[]{var1}, CGLIB$getBook$1$Proxy) : super.getBook(var1); } final boolean CGLIB$equals$2(Object var1) { return super.equals(var1); } public final boolean equals(Object var1) { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { Object var2 = var10000.intercept(this, CGLIB$equals$2$Method, new Object[]{var1}, CGLIB$equals$2$Proxy); return var2 == null ? false : (Boolean)var2; } else { return super.equals(var1); } } final String CGLIB$toString$3() { return super.toString(); } public final String toString() { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$3$Method, CGLIB$emptyArgs, CGLIB$toString$3$Proxy) : super.toString(); } final int CGLIB$hashCode$4() { return super.hashCode(); } public final int hashCode() { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { Object var1 = var10000.intercept(this, CGLIB$hashCode$4$Method, CGLIB$emptyArgs, CGLIB$hashCode$4$Proxy); return var1 == null ? 0 : ((Number)var1).intValue(); } else { return super.hashCode(); } } final Object CGLIB$clone$5() throws CloneNotSupportedException { return super.clone(); } protected final Object clone() throws CloneNotSupportedException { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } return var10000 != null ? var10000.intercept(this, CGLIB$clone$5$Method, CGLIB$emptyArgs, CGLIB$clone$5$Proxy) : super.clone(); } public static MethodProxy CGLIB$findMethodProxy(Signature var0) { String var10000 = var0.toString(); switch (var10000.hashCode()) { case -1644681230: if (var10000.equals("addBook(Lorg/example/domain/Book;)V")) { return CGLIB$addBook$0$Proxy; } break; case -508378822: if (var10000.equals("clone()Ljava/lang/Object;")) { return CGLIB$clone$5$Proxy; } break; case 1332997645: if (var10000.equals("getBook(Ljava/lang/String;)Lorg/example/domain/Book;")) { return CGLIB$getBook$1$Proxy; } break; case 1826985398: if (var10000.equals("equals(Ljava/lang/Object;)Z")) { return CGLIB$equals$2$Proxy; } break; case 1913648695: if (var10000.equals("toString()Ljava/lang/String;")) { return CGLIB$toString$3$Proxy; } break; case 1984935277: if (var10000.equals("hashCode()I")) { return CGLIB$hashCode$4$Proxy; } } return null; } public Librarian$$EnhancerByCGLIB$$e9192983() { CGLIB$BIND_CALLBACKS(this); } public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) { CGLIB$THREAD_CALLBACKS.set(var0); } public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) { CGLIB$STATIC_CALLBACKS = var0; } private static final void CGLIB$BIND_CALLBACKS(Object var0) { Librarian$$EnhancerByCGLIB$$e9192983 var1 = (Librarian$$EnhancerByCGLIB$$e9192983)var0; if (!var1.CGLIB$BOUND) { var1.CGLIB$BOUND = true; Object var10000 = CGLIB$THREAD_CALLBACKS.get(); if (var10000 == null) { var10000 = CGLIB$STATIC_CALLBACKS; if (var10000 == null) { return; } } var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0]; } } public Object newInstance(Callback[] var1) { CGLIB$SET_THREAD_CALLBACKS(var1); Librarian$$EnhancerByCGLIB$$e9192983 var10000 = new Librarian$$EnhancerByCGLIB$$e9192983(); CGLIB$SET_THREAD_CALLBACKS((Callback[])null); return var10000; } public Object newInstance(Callback var1) { CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1}); Librarian$$EnhancerByCGLIB$$e9192983 var10000 = new Librarian$$EnhancerByCGLIB$$e9192983(); CGLIB$SET_THREAD_CALLBACKS((Callback[])null); return var10000; } public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) { CGLIB$SET_THREAD_CALLBACKS(var3); Librarian$$EnhancerByCGLIB$$e9192983 var10000 = new Librarian$$EnhancerByCGLIB$$e9192983; switch (var1.length) { case 0: var10000.<init>(); CGLIB$SET_THREAD_CALLBACKS((Callback[])null); return var10000; default: throw new IllegalArgumentException("Constructor not found"); } } public Callback getCallback(int var1) { CGLIB$BIND_CALLBACKS(this); MethodInterceptor var10000; switch (var1) { case 0: var10000 = this.CGLIB$CALLBACK_0; break; default: var10000 = null; } return var10000; } public void setCallback(int var1, Callback var2) { switch (var1) { case 0: this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2; default: } } public Callback[] getCallbacks() { CGLIB$BIND_CALLBACKS(this); return new Callback[]{this.CGLIB$CALLBACK_0}; } public void setCallbacks(Callback[] var1) { this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0]; } static { CGLIB$STATICHOOK1(); } }
可以看到在代理类的重写方法中,使用了MethodInterceptor拦截器的intercept方法。
到此这篇关于Java中的Cglib动态代理详细解读的文章就介绍到这了,更多相关Cglib动态代理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!