Spring CGLIB 和 CGLIB 有什么区别?

What's the difference between Spring CGLIB and CGLIB?

Spring不包含cglib依赖,cglib和springcglib都有Enhancerclass,一个是net.sf.cglib.proxy.Enhancer,另一个是org.springframework.cglib.proxy,它们有什么区别?

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"

这称为重新打包:项目不是使用某些库作为依赖项,而是将依赖项的副本作为其自己项目的一部分并将其放置在不同的包中。

这样做的原因是使用 Spring 的项目可能想使用 cglib 本身。如果 Spring 有一个特定版本的 cglib 作为依赖项,使用 Spring 的项目将不可能选择不同的版本。但是如果Spring使用不同包中的重新打包的cglib,则没有版本冲突,项目可以使用任何版本的cglib。

一些项目以类似的方式重新打包 Guava、Netty 或其他流行的库。

Cglib 已内联到 Spring 版本 3.2.0 中,如 release notes of this version:

中所述

In prior versions, users of Spring's subclass-based AOP proxies (e.g. via proxy-target-class="true") and @Configuration class support were required to declare an explicit dependency on CGLIB 2.2. As of Spring Framework 3.2, we now repackage and inline the newly-released CGLIB 3.0.

This means greater convenience for users, as well as correct functionality for Java 7 users who are creating subclass proxies of types that contain invokedynamic bytecode instructions. Repackaging CGLIB internally ensures no classpath conflicts with other third party frameworks that may depend on other versions of CGLIB.

这样做是为了提供与 cglib 相关的自动更新并避免版本冲突,因为 cglib 有时会破坏其 API。

没有区别,只是重新包装


ps。 class ObjenesisCglibAopProxy extends CglibAopProxy

Objenesis aims to overcome these restrictions by bypassing the constructor on object instantiation.