在使用 CGLIB 動態代理(如 Spring AOP、Hibernate 增強)時,啟動拋出:
Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
ASM 版本衝突。CGLIB 內部依賴 ASM 字節碼操作庫。若項目傳遞依賴引入了多個版本的 asm.jar(如 asm:asm、asm:asm-commons、cglib:cglib 自帶的舊版 ASM),類加載器載入較舊版本的 ClassWriter,導致方法簽名不匹配。
cglib-nodep(nodep = no dependencies)將 ASM 類重新打包至 org.objectweb.asm → net.sf.cglib.core.asm 命名空間,完全隔離與外部 ASM 依賴,徹底消除衝突。
<!-- 移除舊版 cglib 與獨立 asm 依賴 -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.3.0</version> <!-- 推薦 3.x 版本 -->
</dependency>
implementation 'cglib:cglib-nodep:3.3.0'
mvn dependency:tree | grep asm 確認無 asm:asm 殘留。Spring Boot 2.x 起預設依賴 spring-boot-starter-aop → spring-aop → cglib-nodep,通常無需手動干預。若自行引入 cglib:cglib 需排除傳遞依賴:
<dependency>
<groupId>cglib</groupId><artifactId>cglib</artifactId>
<exclusions><exclusion><groupId>asm</groupId><artifactId>asm</artifactId></exclusion></exclusions>
</dependency>
遇到 ASM 相關 NoSuchMethodError/NoClassDefFoundError,首選升級至 cglib-nodep 3.x。這是社區公認的最穩健方案,避免在依賴地獄中手動排除版本。