What is CGLIB and how it is related to Spring? Do we have to define usage of CGLIB explicitly when using Spring Framework?
Under the covers, it uses ASM bytecode manipulation framework. Essentially, CGLIB dynamically generates a subclass to override the non-final methods of the proxied class. It is faster than the JDK dynamic proxy approach, which uses Java reflection. CGLIB cannot proxy a final class or a class with any final methods.
JDK dynamic proxy is available with the JDK. It can be only proxy by interface so target class needs to implement interface. In your is implementing one or more interface then spring will automatically use JDK dynamic proxies. On the other hand, CGLIB is a third party library which spring used for creating proxy.
From Spring documentation : Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice). If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used.
What is cglib:cglib-nodep? Cglib is a powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and implements interfaces at runtime. cglib:cglib-nodep is a tool in the Maven Packages category of a tech stack.
Ref Spring docs. What is CGLIB & how is it related to Spring?
CGLIB is a code generation 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.
Via 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.
Via 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With