Joke Collection Website - Public benefit messages - The difference between jdk dynamic proxy and cglib
The difference between jdk dynamic proxy and cglib
When one object (client) can't or doesn't want to directly refer to another object (target object), the proxy mode can be applied to build a bridge between two proxy objects.
According to the different creation periods of proxy objects, they can be divided into two types:
Static proxy: the proxy object class is pre-written and existed before the program was released;
Dynamic proxy: After the application is published, the proxy object is created dynamically.
Static proxy is actually a typical proxy mode implementation. It is simple to wrap the proxy object in a proxy class and then influence the behavior of the proxy object, so the code will not be put down.
Dynamic agent can be divided into JDK dynamic agent and CGLIB agent.
1.JDK dynamic proxy
At this time, the proxy object and the target object implement the same interface, and the target object is an attribute of the proxy object. In the concrete interface implementation, other business processing logic can be added before and after calling the corresponding method of the target object.
In practice, the proxy mode needs to specify a specific target object. If you add a proxy class to each class, many classes will be generated. At the same time, how to realize the proxy mode without knowing the specific class? This leads to dynamic proxy.
JDK dynamic proxy can only generate proxies for classes that implement interfaces.
2.CGLIB agent
CGLIB (Code Generalization Library) proxy is the proxy of class implementation,
It mainly generates a subclass of the specified class, covering all the methods in it, so the class or method cannot be declared as final.
The difference between JDK dynamic proxy and CGLIB proxy generation
JDK dynamic proxy can only generate proxies for classes that implement interfaces, but not for classes.
CGLIB is a proxy for class implementation, which mainly generates subclasses of specified classes and covers the methods in them.
Because it is inheritance, it is best not to declare a class or method as final, which can prevent inheritance and polymorphism.
PS: final modified data has the characteristics of "final state", which means "final":
The final decorated class cannot be inherited.
The final modified method cannot be overridden by subclasses.
A variable (member variable or local variable) decorated by final becomes a constant and can only be assigned once.
Member variables decorated with final must be assigned values at the same time when declared. If you don't assign values to them when you declare them, you only have one chance to assign values, and they can only be used if they are explicitly assigned values in the constructor.
Local variables decorated with final can only be declared without assignment, and then assigned once.
Reference code
CGLIB:?
12345678 public object createProxyObject(Object obj){? this.targetObject = obj? Enhancer enhancer = new enhancer (); ? enhancer . set superclass(obj . getclass()); ? enhancer . set callback(this); ? object proxy obj = enhancer . create(); ? Returns proxyObj// returns the proxy object. The returned object is actually a proxy class that encapsulates the "implementation class" and is an instance of the implementation class. ? } ? JDK:?
12345 public object new proxy {//The target object of the incoming proxy? this . target object = target object; ? & ltbr & gt? //Note the parameters of this method, followed by the interface returnproxy implemented by the class. newproxyinstance(target object。 getclass()。 getclassloader(),? targetObject.getClass()。 getInterfaces(),this); //Return the proxy object? } As you can see in the code, when the proxy class is generated, the interface targetObject. getClass()。 GetInterfaces () implemented by the implementation class is passed, so the JDK can only act as a proxy for the interface. If it is changed to a class, a java.lang.ClassCastException exception will be thrown. ?
In the source code of Spring, you can see a lot of code that generates proxy classes.
Application of dynamic agent
AOP(aspect-oriented programming) includes Aspect, advice and joinpoint, which is realized by adding a notice before and after the join point of the proxy of the target object to complete the unified cutting operation.
The technologies to realize AOP are mainly divided into two categories:
One is to use dynamic proxy technology to decorate messages by intercepting them instead of executing the original object behavior;
Secondly, static weaving is used to create "aspects" by introducing specific syntax, so that the compiler can weave the code about "aspects" at compile time.
Spring provides two methods to generate proxy objects: JDKProxy and Cglib. The specific way to generate proxy objects is determined by AopProxyFactory according to the configuration of AdvisedSupport objects.
If the target class is an interface, the default strategy is to use JDK dynamic proxy technology; If the target object does not implement an interface, the default policy is to use the CGLIB proxy.
If the target object implements the interface, it can be forced to use CGLIB to implement the proxy (add CGLIB library and add
- Related articles
- Why can't I send a text message after the US version of Apple 5 is swiped?
- How does Xiaomi mobile phone retrieve deleted photos?
- Love rat's Quotations in 2020: love rat's Unified Statement.
- Is it true that the pre-litigation notice was sent by SMS?
- How to do a good job in the campus WeChat public platform?
- What's the charge for sending messages to Australia? It is included in the free SMS package.
- Smoking is harmful to health and the content of the handwritten newspaper
- What does the short message sent by the bank mean, and the overdraft must be submitted to the basic database of financial credit information?
- The Basic Functions of Cool Sound Xiu Xiu
- Which collection is the SMS of consumption installment?