Java中的cglib代理詳解
工作原理
使用
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>對(duì)類和接口分別進(jìn)行代理
DemoService
package com.fanqiechaodan.user.service;
/**
* @author fanqiechaodan
* @Classname DemoService
* @Description
* @Date 2023/3/1 19:44
*/
public class DemoService {
public void test(){
System.out.println("==================>test");
}
}DemoInterface
package com.fanqiechaodan.user.service;
/**
* @author fanqiechaodan
* @Classname DemoInterface
* @Description
* @Date 2023/3/1 19:56
*/
public interface DemoInterface {
void test();
}Demo
package com.fanqiechaodan.user;
import com.fanqiechaodan.user.service.DemoInterface;
import com.fanqiechaodan.user.service.DemoService;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
/**
* @author fanqiechaodan
* @Classname Main
* @Description
* @Date 2023/3/1 19:47
*/
public class Demo {
public static void main(String[] args) {
final DemoService target = new DemoService();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(DemoService.class);
// 必須設(shè)置callbacks不然會(huì)出現(xiàn)java.lang.NullPointerException
enhancer.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> {
// test方法增強(qiáng)邏輯
if ("test".equals(method.getName())) {
System.out.println("方法執(zhí)行前增強(qiáng)...");
Object res = method.invoke(target, objects);
System.out.println("方法執(zhí)行后增強(qiáng)...");
return res;
}
// 其他方法正常執(zhí)行
return method.invoke(target, objects);
}});
DemoService demoService = (DemoService) enhancer.create();
demoService.test();
Enhancer enhancer1 = new Enhancer();
enhancer1.setSuperclass(DemoInterface.class);
enhancer1.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> {
// test方法增強(qiáng)邏輯
if ("test".equals(method.getName())) {
System.out.println("切面邏輯...");
}
return null;
}});
DemoInterface demo = (DemoInterface) enhancer1.create();
demo.test();
}
}代碼執(zhí)行前設(shè)置VM options:
-Dcglib.debugLocation=根據(jù)自己實(shí)際情況填寫
運(yùn)行代碼是cglib就會(huì)將生成的代理類放到上面所指定的路徑上.

DemoService代理類
public class DemoService$$EnhancerByCGLIB$$1c9ab053 extends DemoService implements Factory {
// 省去不重要代碼...
final void CGLIB$test$0() {
super.test();
}
public final void test() {
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$test$0$Method, CGLIB$emptyArgs, CGLIB$test$0$Proxy);
} else {
super.test();
}
}
// 省去不重要代碼...
}DemoInterface代理類
public class DemoInterface$$EnhancerByCGLIB$$536bfe99 implements DemoInterface, Factory {
// 省去不重要代碼...
final void CGLIB$test$4() {
super.test();
}
public final void test() {
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$test$4$Method, CGLIB$emptyArgs, CGLIB$test$4$Proxy);
} else {
super.test();
}
}
// 省去不重要代碼...
} final void CGLIB$test$0() {
super.test();
}這個(gè)方法我們并不能直接調(diào)用,而是要通過(guò)所設(shè)置的Callback,也就是MethodInterceptor中的methodProxy來(lái)調(diào)用,methodProxy表示方法代理,假如DemoService代理對(duì)象在執(zhí)行test()方法,那么當(dāng)執(zhí)行流程進(jìn)入到intercept方法時(shí),methodProxy表示的就是test()方法,DemoService和DemoService代理類中都有test(),所以methodProxy代理的就是這兩個(gè)test()

工作原理:cglib會(huì)根據(jù)所設(shè)置得superclass,生成代理類作為其子類,并且會(huì)重寫superclass中需要代理得方法,比如test(),相應(yīng)得再代理類中會(huì)有兩個(gè)方法,一個(gè)是重寫的test(),用來(lái)執(zhí)行增強(qiáng)邏輯,一個(gè)是CGLIB$test$0(),會(huì)直接調(diào)用super.test(),是讓MethodProxy對(duì)象來(lái)用的
代理類生成邏輯
- DemoService代理類全部代碼
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.fanqiechaodan.user.service;
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;
public class DemoService$$EnhancerByCGLIB$$1c9ab053 extends DemoService 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$test$0$Method;
private static final MethodProxy CGLIB$test$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$equals$1$Method;
private static final MethodProxy CGLIB$equals$1$Proxy;
private static final Method CGLIB$toString$2$Method;
private static final MethodProxy CGLIB$toString$2$Proxy;
private static final Method CGLIB$hashCode$3$Method;
private static final MethodProxy CGLIB$hashCode$3$Proxy;
private static final Method CGLIB$clone$4$Method;
private static final MethodProxy CGLIB$clone$4$Proxy;
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("com.fanqiechaodan.user.service.DemoService$$EnhancerByCGLIB$$1c9ab053");
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$1$Method = var10000[0];
CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
CGLIB$toString$2$Method = var10000[1];
CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
CGLIB$hashCode$3$Method = var10000[2];
CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$3");
CGLIB$clone$4$Method = var10000[3];
CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
CGLIB$test$0$Method = ReflectUtils.findMethods(new String[]{"test", "()V"}, (var1 = Class.forName("com.fanqiechaodan.user.service.DemoService")).getDeclaredMethods())[0];
CGLIB$test$0$Proxy = MethodProxy.create(var1, var0, "()V", "test", "CGLIB$test$0");
}
final void CGLIB$test$0() {
super.test();
}
public final void test() {
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$test$0$Method, CGLIB$emptyArgs, CGLIB$test$0$Proxy);
} else {
super.test();
}
}
final boolean CGLIB$equals$1(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$1$Method, new Object[]{var1}, CGLIB$equals$1$Proxy);
return var2 == null ? false : (Boolean)var2;
} else {
return super.equals(var1);
}
}
final String CGLIB$toString$2() {
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$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy) : super.toString();
}
final int CGLIB$hashCode$3() {
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$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
return var1 == null ? 0 : ((Number)var1).intValue();
} else {
return super.hashCode();
}
}
final Object CGLIB$clone$4() 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$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy) : super.clone();
}
public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch(var10000.hashCode()) {
case -1422510685:
if (var10000.equals("test()V")) {
return CGLIB$test$0$Proxy;
}
break;
case -508378822:
if (var10000.equals("clone()Ljava/lang/Object;")) {
return CGLIB$clone$4$Proxy;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return CGLIB$equals$1$Proxy;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return CGLIB$toString$2$Proxy;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return CGLIB$hashCode$3$Proxy;
}
}
return null;
}
public DemoService$$EnhancerByCGLIB$$1c9ab053() {
CGLIB$BIND_CALLBACKS(this);
}
// 將代碼中設(shè)置的callbacks設(shè)置到CGLIB$THREAD_CALLBACKS這個(gè)ThreadLocal中
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) {
DemoService$$EnhancerByCGLIB$$1c9ab053 var1 = (DemoService$$EnhancerByCGLIB$$1c9ab053)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);
DemoService$$EnhancerByCGLIB$$1c9ab053 var10000 = new DemoService$$EnhancerByCGLIB$$1c9ab053();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
DemoService$$EnhancerByCGLIB$$1c9ab053 var10000 = new DemoService$$EnhancerByCGLIB$$1c9ab053();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
DemoService$$EnhancerByCGLIB$$1c9ab053 var10000 = new DemoService$$EnhancerByCGLIB$$1c9ab053;
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();
}
}代理類中有一個(gè)靜態(tài)代碼塊,會(huì)調(diào)用CGLIB$STATICHOOK1(),這個(gè)方法主要是為了給屬性賦值,例如:
- 構(gòu)造一個(gè)ThreadLocal對(duì)象賦值給CGLIB$THREAD_CALLBACKS
- 獲取DemoService類中的test方法的Method對(duì)象賦值給CGLIB$test0 00Method
- 構(gòu)造test()方法所對(duì)應(yīng)的MethodProxy對(duì)象賦值給CGLIB$test0 00Proxy
DemoService的代理類即繼承了DemoService,也實(shí)現(xiàn)了Factory接口,從而就需要實(shí)現(xiàn)Factory接口中的方法
public interface Factory {
Object newInstance(Callback callback);
Object newInstance(Callback[] callbacks);
Object newInstance(Class[] types, Object[] args, Callback[] callbacks);
Callback getCallback(int index);
void setCallback(int index, Callback callback);
void setCallbacks(Callback[] callbacks);
Callback[] getCallbacks();
}newInstance方法會(huì)重新生成一個(gè)代理對(duì)象,setCallbacks()和getCallbacks()用來(lái)設(shè)置或獲取增強(qiáng)邏輯
代理類大致流程是:
- cglib先生成代理類,具體邏輯為:
- 先生成類的定義,繼承DemoService和實(shí)現(xiàn)Factory
- 根據(jù)DemoService類中的方法生成代理類中對(duì)應(yīng)的方法和屬性
- 生成一些輔助的屬性和方法
- 然后調(diào)用構(gòu)造方法得到代理對(duì)象
- 然后cglib調(diào)用代理對(duì)象的CGLIB$SET_THREAD_CALLBACKS()方法,將我們自己所設(shè)置的callbacks設(shè)置到CGLIB$THREAD_CALLBACKS這個(gè)ThreadLocal中
- 后續(xù)代理對(duì)象再執(zhí)行test()方法時(shí),就會(huì)從CGLIB$THREAD_CALLBACKS拿到所設(shè)置的callbacks,調(diào)用其intercept()方法
到此這篇關(guān)于Java中的cglib代理詳解的文章就介紹到這了,更多相關(guān)Java的cglib代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java編寫簡(jiǎn)易rabbitmq生產(chǎn)者與消費(fèi)者的代碼
開發(fā)時(shí)經(jīng)常與其它系統(tǒng)用rabbitmq對(duì)接,當(dāng)需要自測(cè)時(shí),還是自己寫rabbitmq生產(chǎn)者、消費(fèi)者自測(cè)方便些,下面給大家總結(jié)使用java編寫簡(jiǎn)易rabbitmq的方法,感興趣的朋友一起看看吧2023-11-11
application.yaml與bootstrap.yaml的使用
這篇文章主要介紹了application.yaml與bootstrap.yaml的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Spring boot 數(shù)據(jù)庫(kù)連接斷線重連問(wèn)題
這篇文章主要介紹了Spring boot 數(shù)據(jù)庫(kù)連接斷線重連問(wèn)題,需要的朋友可以參考下2017-06-06
SpringBoot項(xiàng)目啟動(dòng)失?。篈PPLICATION FAILED TO START的
文章介紹了在使用Spring Boot 4.0.0版本時(shí)遇到的Mapper文件找不到的問(wèn)題,并通過(guò)將依賴版本切換到3.5.7來(lái)解決問(wèn)題,此外,文章還提供了關(guān)于Spring Boot新版本使用注意事項(xiàng)的總結(jié),包括版本兼容性、配置變更、新特性、遷移建議和常見問(wèn)題,需要的朋友可以參考下2025-11-11
Java JNA庫(kù)詳解與本地系統(tǒng)交互實(shí)戰(zhàn)記錄(推薦)
JNA 是一個(gè)開源庫(kù),它提供了 Java 程序調(diào)用本地共享庫(kù)(如 DLLs 在 Windows 或 .so 文件在 Linux/Unix)的功能,本文給大家介紹Java JNA庫(kù)詳解與本地系統(tǒng)交互實(shí)戰(zhàn),感興趣的朋友一起看看吧2025-11-11
關(guān)于SpringBoot+Mybatis報(bào)MapperScan.factoryBean()問(wèn)題
解決SpringBoot+Mybatis中的MapperScan.factoryBean()問(wèn)題,讓你的項(xiàng)目運(yùn)行更順暢!本指南將帶你一步步解決這個(gè)問(wèn)題,讓你的開發(fā)過(guò)程更加高效,不要錯(cuò)過(guò)這個(gè)實(shí)用指南,快來(lái)一探究竟吧!2024-02-02
Spring中的@Value和@PropertySource注解詳解
這篇文章主要介紹了Spring中的@Value和@PropertySource注解詳解,@PropertySource:讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中,本文提供了部分實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-11-11

