Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is CGLIB in Spring Framework? [closed]

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

like image 717
Punter Vicky Avatar asked Jun 29 '16 02:06

Punter Vicky


People also ask

How does Cglib proxy work?

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.

What is the difference between JDK dynamic proxy and Cglib?

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.

How are JDK proxies and Cglib proxies used in Spring?

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 Nodep?

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.


1 Answers

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.

like image 63
Lovababu Avatar answered Sep 28 '22 01:09

Lovababu