The background,

Spring AOP JDK or CGLib dynamic proxy which is more efficient? In the knowledge of the planet sorted out, today special share, for everyone’s reference! For those interested in knowledge Planet, check out:

2. Basic concepts

First, we know that the underlying implementation of Spring AOP comes in two ways: JDK dynamic proxies and CGLib.

Since Java 1.3, Java has provided dynamic proxy technology, which allows developers to create proxy instances of interfaces at run time, and has since been used in many parts of Spring.

JDK dynamic proxies mainly involve two classes under the java.lang.Reflect package: Proxy and InvocationHandler. Among them, InvocationHandler is an interface that dynamically weaves crosscutting logic and business logic together by implementing the interface that defines crosscutting logic and invokes the code of the target class through reflection.

JDK dynamic proxy has a limitation that it can only create proxy instances for interfaces. For classes that do not define business methods through interfaces, how to create dynamic proxy instances? The answer is CGLib.

CGLib uses the underlying bytecode technology, Code Generation Library. CGLib can create a subclass for a class. In the subclass, method interception techniques are used to intercept all calls to the methods of the parent class and weave crosscutting logic into them.

JDK and CGLib dynamic proxy

1. Specific implementation principle of JDK dynamic proxy:

  • Create your own call handler by implementing the InvocationHandler interface;

  • Create a dynamic Proxy by specifying a ClassLoader object and a set of interfaces for the Proxy class;

  • Get the constructor of the dynamic proxy class through reflection, whose only parameter type is the calling processor interface type;

  • Create a dynamic proxy class instance using a constructor that calls a handler object as an argument.

The JDK dynamic proxy is an interface oriented proxy pattern, and Spring can’t do much about it if the promanaged target doesn’t have an interface. Spring uses Java’s reflection mechanism to produce a new anonymous implementation class for the promanaged interface, overwriting the AOP enhancements.

2. CGLib dynamic proxy:

CGLib is a powerful, high-performance Code production class library that can achieve runtime dynamic extension of Java classes. Spring inherits classes to be dynamically proxied at runtime through CGLib, overriding the methods of the parent class, and implementing AOP faceted programming.

3. Comparison between the two:

  • JDK dynamic proxies are interface oriented.

  • CGLib dynamic proxies are implemented through bytecode low-level inheritance of the proxy class (which will fail if the proxy class is decorated with the final keyword).

4. Use caution:

  • If the object to be propped is an implementation class, Spring uses JDK dynamic proxies (Spirng uses JDK dynamic proxies by default).

  • If the proxied object is not an implementation class, Spring forces the use of CGLib to implement dynamic proxiing.

JDK and CGLib dynamic proxy performance comparison – textbook description

Whether we read a book or an article or search for reference answers on my website, we can find the following answers in many cases:

In terms of performance between the two, JDK dynamic proxies created by proxy objects, in previous JDK versions, performance is not very high, although the performance of JDK dynamic proxies has been greatly improved in older versions, but it is not suitable for all scenarios. It is mainly reflected in the following two indicators:

1. The performance of dynamic proxy objects created by CGLib is much higher than that of JDK dynamic proxy. Some studies show that the performance of dynamic proxy objects is about 10 times higher than that of JDK dynamic proxy objects.

However, CGLib takes up to 8 times more time to create objects than JDK dynamic proxies.

Therefore, CGLib dynamic proxies are more suitable for singleton or instance pool proxies because there is no need to create proxy objects frequently.

Are the results as described in 1, 2 and 3 above? Let’s do some small experiments to analyze it!

Five, performance test

First, there are a few Java classes

2, Target. Java

3, TargetImpl. Java

4, JdkDynamicProxyTest. Java

5, CglibProxyTest. Java

6, ProxyPerformanceTest. Java

7. Test results

(1) JDK 1.6

(2) JDK 1.7

(3) JDK 1.8

After many experiments, it can be seen that the average case, the JDK dynamic proxy running speed has been improved gradually, at the time of low version, run of the performance may not be additional, but run many times in the 1.8 version, the basic can be consistent with the test results, it is the JDK dynamic proxy is additional dynamic proxy is fast!

However, JDK dynamic proxy and CGLib dynamic proxy application scenarios are still different.

Six, summarized

At 1.6 and 1.7, JDK dynamic proxies were slower than CGLib dynamic proxies, but not by a factor of 10. In JDK1.8, JDK dynamic proxies were already faster than CGLib dynamic proxies. Hope small partner can have a definite aim when meeting this problem!

The JDK and CGLib dynamic proxy in Spring AOP have been tested and tested with a preliminary result. If someone asks you about Spring AOP, do not simply say JDK dynamic proxy and CGLib these two. It is time to throw out the understanding of the difference between the two is a plus oh!

Reference article:

1, 2, https://blog.csdn.net/xiangbq/article/details/49794335, https://blog.csdn.net/qq1723205668/article/details/56481476

Click on the image to see more recommendations

Left left left

MySQL Database tips for you!

Dynamic proxy whip out! Take a look at the underlying implementation principle of MyBatis!

Daunting source code, how to look at it?

How just calculate a reliable programmer!