In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what is the difference between JDK dynamic agent and CGLIB dynamic agent, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Difference:
Based on the reflection mechanism, JDK generates an anonymous class that implements the proxy interface, and then rewrites the method to enhance the method.
It generates classes quickly, but because the runtime is reflection-based, it is slow to call subsequent class operations.
And he can only program against the interface.
CGLIB is based on the inheritance mechanism, inheriting the proxied class, so the method should not be declared as final, and then rewrite the parent class method to enhance the function of the class.
Its bottom layer is based on the asm third-party framework, which loads the class file of the proxy object class and modifies its byte code to generate a subclass to deal with it.
The generation of the class is slow, but the subsequent operation of the class is fast.
Can be targeted at classes and interfaces.
Differences in performance:
Because jdk is based on reflection, CGLIB is based on bytecode. So there will be differences in performance.
In the old version, CGLIB was about 10 times faster than JDK, but the CGLIB startup class was about 8 times slower than JDK, but in fact, the speed of JDK improved a lot every time the version was upgraded, while CGLIB still stood still.
In the code experiments of JDK dynamic agent and CGlib dynamic agent, the dynamic agent performance of JDK7 and 8 is about 20% better than that of CGlib under 1W execution.
Achieve:
1. The concrete realization principle of JDK dynamic agent:
Create your own call resolver by implementing the InvocationHandler interface
Create a dynamic agent by specifying a ClassLoader object and a set of interface for the Proxy class
The constructor of the dynamic agent class is obtained through the reflection mechanism, and its only parameter type is to call the resolver interface type.
Create a dynamic agent class instance through the constructor, and use the resolver object as a parameter to construct the time tone.
JDK dynamic agent is an interface-oriented agent model, if the target of the agent does not have an interface, then Spring is powerless. Spring produces a new anonymous implementation class of the agent interface through the reflection mechanism of Java, rewriting the strengthening method of AOP.
2. CGLib dynamic agents:
CGLib is a powerful, high-performance Code production class library, which can dynamically extend java classes at run time. Spring inherits the classes to be dynamically represented by CGlib during the run time, rewrites the methods of the parent class, and realizes AOP aspect-oriented programming.
How to enforce the use of CGLIB to implement AOP?
(1) add CGLIB library, SPRING_HOME/cglib/*.jar
(2) add to the spring configuration file
What is the difference between JDK dynamic proxy and CGLIB bytecode generation?
(1) JDK dynamic proxy can only generate proxies for classes that implement interfaces, but not for classes.
(2) CGLIB implements a proxy for a class, which mainly generates a subclass for the specified class and overrides the methods in it
Because it is inheritance, it is best not to declare this class or method as final
Summary:
Spring AOP in the JDK and CGLib dynamic agents on this knowledge point is very important, on the performance between the two tests have already had a preliminary result, later someone asked you Spring AOP, do not simply say JDK dynamic agents and CGLib these two, it is time to throw out the understanding of the difference between the two, there are points oh!
About JDK dynamic agent and CGLIB dynamic agent what is the difference to share here, I hope the above content can be of some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.