【发布时间】:2019-12-04 14:55:45
【问题描述】:
Spring不包含cglib依赖,cglib和spring cglib都有Enhancer类,一个是net.sf.cglib.proxy.Enhancer,一个是org.springframework.cglib.proxy,它们有什么区别?
【问题讨论】:
Spring不包含cglib依赖,cglib和spring cglib都有Enhancer类,一个是net.sf.cglib.proxy.Enhancer,一个是org.springframework.cglib.proxy,它们有什么区别?
【问题讨论】:
这称为重新打包:项目不使用某些库作为依赖项,而是将依赖项的副本作为自己项目的一部分并将其放置在不同的包中。
这样做的原因是使用 Spring 的项目可能想要使用 cglib 本身。如果 Spring 有一个特定版本的 cglib 作为依赖项,那么使用 Spring 的项目就不可能选择不同的版本。但是如果 Spring 使用重新打包的 cglib 并且在不同的包中,则不会出现版本冲突,并且项目可以根据需要使用任何版本的 cglib。
一些项目以类似的方式重新打包 Guava、Netty 或其他流行的库。
【讨论】:
Cglib 自版本 3.2.0 起内联到 Spring,正如 release notes of this version 中提到的那样:
在以前的版本中,Spring 的基于子类的 AOP 代理的用户(例如 通过 proxy-target-class="true") 和 @Configuration 类支持是 需要声明对 CGLIB 2.2 的显式依赖。从春天开始 框架 3.2,我们现在重新打包并内联新发布的 CGLIB 3.0.
这意味着为用户提供了更大的便利,以及正确的 为创建子类代理的 Java 7 用户提供的功能 包含调用动态字节码指令的类型。重新包装 CGLIB 在内部确保不会与其他第三方发生类路径冲突 可能依赖于其他版本的 CGLIB 的框架。
这样做是为了提供与 cglib 相关的自动更新并避免版本冲突,因为 cglib 有时会破坏其 API。
【讨论】:
Spring 附带重新打包的 cglib。您可以在 Gradle buildfile 中查看实际的 cglib 版本。搜索单词“cglib”,你会找到它:
// As of Spring 4.0.3, spring-core includes asm 5.x and repackages cglib 3.2, inlining
// both into the spring-core jar. cglib 3.2 itself depends on asm 5.x and is therefore
// further transformed by the JarJar task to depend on org.springframework.asm; this
// avoids including two different copies of asm unnecessarily.
def cglibVersion = "3.2.4"
【讨论】:
Apr 2022,当前Spring 5.3.x 正在重新打包CGLIB 3.3.0 github.com/spring-projects/spring-framework/blob/5.3.x/…,这是当前最新版本的CGLIB (github.com/cglib/cglib/releases)
没有区别,只是重新包装
ps。 class ObjenesisCglibAopProxy extends CglibAopProxy
Objenesis aims to overcome these restrictions by bypassing the constructor on object instantiation.
【讨论】: