Upgrade to CGLIB 3 and inline into spring-core

CGLIB 3 has been released in order to depend on ASM 4, which Spring now
depends on internally (see previous commit).

This commit eliminates spring-beans' optional dependency on cglib-nodep
v2.2 and instead repackages net.sf.cglib => org.springframework.cglib
much in the same way we have historically done with ASM.

This change is beneficial to users in several ways:

 - Eliminates the need to manually add CGLIB to the application
   classpath; especially important for the growing number of
   @Configuration class users. Java-based configuration functionality,
   along with proxy-target-class and method injection features now
   work 'out of the box' in Spring 3.2.

 - Eliminates the possibility of conflicts with other libraries that
   may dependend on differing versions of CGLIB, e.g. Hibernate
   3.3.1.ga and its dependency on CGLIB 2.1.3 would easily cause a
   conflict if the application were depending on CGLIB 3 for
   Spring-related purposes.

 - Picks up CGLIB 3's changes to support ASM 4, meaning that CGLIB is
   that much less likely to work well in a Java 7 environment due to
   ASM 4's support for transforming classes with invokedynamic
   bytecode instructions.

On CGLIB and ASM:

  CGLIB's own dependency on ASM is also transformed along the way to
  depend on Spring's repackaged org.springframework.asm, primarily to
  eliminate unnecessary duplication of ASM classfiles in spring-core and
  in the process save around 100K in the final spring-core JAR file size.

  It is coincidental that spring-core and CGLIB currently depend on the
  exact same version of ASM (4.0), but it is also unlikely to change any
  time soon. If this change does occur and versions of ASM drift, then
  the size optimization mentioned above will have to be abandoned. This
  would have no compatibility impact, however, so this is a reasonable
  solution now and for the forseeable future.

On a mysterious NoClassDefFoundError:

  During the upgrade to CGLIB 3.0, Spring test cases began failing due to
  NoClassDefFoundErrors being thrown from CGLIB's DebuggingClassWriter
  regarding its use of asm-util's TraceClassVisitor type. previous
  versions of cglib-nodep, particularly 2.2, did not cause this behavior,
  even though cglib-nodep has never actually repackaged and bundled
  asm-util classes. The reason for these NoClassDefFoundErrors occurring
  now is still not fully understood, but appears to be due to subtle JVM
  bytecode preverification rules. The hypothesis is that due to minor
  changes in DebuggingClassWriter such as additional casts, access to
  instance variables declared in the superclass, and indeed a change in
  the superclass hierarchy, preverification may be kicking in on the
  toByteArray method body, at which point the reference to the missing
  TraceClassVisitor type is noticed and the NCDFE is thrown. For this
  reason, a dummy implementation of TraceClassVisitor has been added to
  spring-core in the org.springframework.asm.util package. This class
  simply ensures that Spring's own tests never result in the NCDFE
  described above, and more importantly that Spring's users never
  encounter the same.

Other changes include:

 - rename package-private Cglib2AopProxy => CglibAopProxy
 - eliminate all 'cglibAvailable' checks, warnings and errors
 - eliminate all 'CGLIB2' language in favor of 'CGLIB'
 - eliminate all mention in reference and java docs of needing to add
   cglib(-nodep) to one's application classpath

Issue: SPR-9669
This commit is contained in:
Chris Beams
2012-08-08 10:32:55 +02:00
parent c16f18a5fd
commit 92500ab902
24 changed files with 287 additions and 164 deletions

View File

@@ -1177,8 +1177,10 @@ public interface IntroductionInfo {
</listitem>
<listitem>
<para>You'll need the CGLIB 2 binaries on your classpath; dynamic
proxies are available with the JDK.</para>
<para>There is no need to add CGLIB to your classpath. As of Spring
3.2, CGLIB is repackaged and included in the spring-core JAR. In
other words, CGLIB-based AOP will work "out of the box" just as do
JDK dynamic proxies.</para>
</listitem>
</itemizedlist>

View File

@@ -2549,10 +2549,12 @@ public @interface Idempotent {
</listitem>
<listitem>
<para>You will need the CGLIB 2 binaries on your classpath, whereas
dynamic proxies are available with the JDK. Spring will automatically
warn you when it needs CGLIB and the CGLIB library classes are not
found on the classpath.</para>
<para>As of Spring 3.2, it is no longer necessary to add CGLIB to your
project classpath, as CGLIB classes are repackaged under
org.springframework and included directly in the spring-core JAR. This
means that CGLIB-based proxy support 'just works' in the same way that
JDK dynamic proxies always have.
</para>
</listitem>
<listitem>

View File

@@ -1491,14 +1491,18 @@ public class CommandManager implements ApplicationContextAware {
method.<!--Note was plain text; I made it a note and moved it up.--></para>
<note>
<para>For this dynamic subclassing to work, you must have the CGLIB
jar(s) in your classpath. The class that the Spring container will
subclass cannot be <literal>final</literal>, and the method to be
overridden cannot be <literal>final</literal> either. Also, testing a
class that has an <literal>abstract</literal> method requires you to
subclass the class yourself and to supply a stub implementation of the
<literal>abstract</literal> method. Finally, objects that have been
the target of method injection cannot be serialized.</para>
<para>For this dynamic subclassing to work, the class that the Spring
container will subclass cannot be <literal>final</literal>, and the
method to be overridden cannot be <literal>final</literal> either. Also,
testing a class that has an <literal>abstract</literal> method requires
you to subclass the class yourself and to supply a stub implementation
of the <literal>abstract</literal> method. Finally, objects that have
been the target of method injection cannot be serialized. As of Spring
3.2 it is no longer necessary to add CGLIB to your classpath, because
CGLIB classes are repackaged under org.springframework and distributed
within the spring-core JAR. This is done both for convenience as well
as to avoid potential conflicts with other projects that use differing
versions of CGLIB.</para>
</note>
<para>Looking at the <classname>CommandManager</classname> class in the

View File

@@ -885,15 +885,14 @@ public class AppConfig {
comes in: All <literal>@Configuration</literal> classes are subclassed at
startup-time with <literal>CGLIB</literal>. In the subclass, the child
method checks the container first for any cached (scoped) beans before it
calls the parent method and creates a new instance. </para>
calls the parent method and creates a new instance. Note that as of Spring
3.2, it is no longer necessary to add CGLIB to your classpath because
CGLIB classes have been repackaged under org.springframework and included
directly within the spring-core JAR.</para>
<note>
<para> The behavior could be different according to the scope of your
bean. We are talking about singletons here. </para>
</note>
<note>
<para> Beware that, in order for JavaConfig to work, you must include the
CGLIB jar in your list of dependencies. </para>
</note>
<note>
<para> There are a few restrictions due to the fact that CGLIB dynamically
adds features at startup-time: <itemizedlist>

View File

@@ -422,10 +422,7 @@
<para>To create such a proxy, you insert a child
<literal>&lt;aop:scoped-proxy/&gt;</literal> element into a scoped bean
definition.
<!--To create what such proxy? Is the proxy created above? Also, below added an x-ref that seems relevant.-->(If
you choose class-based proxying, you also need the CGLIB library in your
classpath. See <xref
definition. See <xref
linkend="beans-factory-scopes-other-injection-proxies"/> and <xref
linkend="xsd-config"/>.) Why do definitions of beans scoped at the
<literal>request</literal>, <literal>session</literal>,
@@ -502,9 +499,7 @@
<para>By default, when the Spring container creates a proxy for a bean
that is marked up with the
<literal>&lt;aop:scoped-proxy/&gt;</literal> element, <emphasis>a
CGLIB-based class proxy is created</emphasis>. This means that you
need to have the CGLIB library in the classpath of your
application.</para>
CGLIB-based class proxy is created</emphasis>.</para>
<para><emphasis>Note: CGLIB proxies only intercept public method
calls!</emphasis> Do not call non-public methods on such a proxy; they

View File

@@ -1161,8 +1161,12 @@ public interface IntroductionInfo {
</listitem>
<listitem>
<para>You'll need the CGLIB 2 binaries on your classpath; dynamic
proxies are available with the JDK.</para>
<para>As of Spring 3.2 it is no longer required to add CGLIB to your
project classpath. CGLIB classes have been repackaged under
org.springframework and included directly in the spring-core JAR. This
is both for user convenience as well as to avoid potential conflicts
with other projects that have dependence on a differing version of
CGLIB.</para>
</listitem>
</itemizedlist>

View File

@@ -515,17 +515,11 @@ class GroovyMessenger implements Messenger {
<para>
The JRuby scripting support in Spring requires the following
libraries to be on the classpath of your application.
(The versions listed just happen to be the versions that the
Spring team used in the development of the JRuby scripting support;
you may well be able to use another version of a specific library.)
</para>
<itemizedlist>
<listitem>
<para><filename>jruby.jar</filename></para>
</listitem>
<listitem>
<para><filename>cglib-nodep-2.1_3.jar</filename></para>
</listitem>
</itemizedlist>
</sidebar>
<para>From the JRuby homepage...</para>
@@ -803,9 +797,6 @@ public class Main {
<listitem>
<para><filename>bsh-2.0b4.jar</filename></para>
</listitem>
<listitem>
<para><filename>cglib-nodep-2.1_3.jar</filename></para>
</listitem>
</itemizedlist>
</sidebar>
<para>From the BeanShell homepage...</para>