What is CGLIB in Spring Framework? [closed]

2019-04-04 14:23发布

问题:

What is CGLIB and how it is related to Spring? Do we have to define usage of CGLIB explicitly when using Spring Framework?

回答1:

Ref Spring docs. What is CGLIB & how is it related to Spring?

CGLIB is a code genration library. Spring uses CGLIB, to generate proxies.

Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.

Yes you have to tell spring to use CGLIB based proxies explicitly.

Through xml:

<aop:aspectj-autoproxy proxy-target-class="true"/> proxy-target-class property is set to true will cause CGLIB-based proxying to be in effect.

Through Annotation:

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AppConfig {
   // ...
}

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.

You may have look at this too.