Definition and difference between static proxy and two dynamic proxy in java

1. Agency model

Proxy (Proxy) is one of java design patterns, providing another way to access the target object; that is accessed by the target through a proxy object.
The advantage of this is : on the basis of the target object can be achieved on additional enhanced Functional operation, that is, expand the function of the target object.

1.1.Static proxyWhen
using a static proxy, you need to define an interface or a parent class. The proxy object and the proxy object implement the same interface or inherit the same parent class.

2. Dynamic proxy
2.1 Dynamic proxy (Method 1)
Dynamic proxy has the following characteristics:
1. Proxy object, does not need to implement the interface
2. The generation of proxy object is to use the JDK API to dynamically build the proxy object in memory (requires us Specify the type of interface implemented by the created proxy object/target object)
3. Dynamic proxy is also called: JDK proxy, interface proxy

2.2. Cglib proxy (method two) The
above static proxy and dynamic proxy both require that the target object is the target object that implements an interface , but sometimes the target object is just a single object and does not implement any interface, this time you can Use the method of subclassing the target object to implement the proxy. This method is called: Cglib proxy.

Cglib proxy, also called subclass proxy, is to build a subclass object in memory to achieve the expansion of the target object function.
Cglib is a powerful high-performance code generation package, which can extend java classes and Implement java interface. It is widely used by many AOP frameworks, such as Spring AOP and synaop. The interception (interception) that provides methods for them
is the bottom layer of the Cglib package by using a small and block bytecode processing framework ASM to convert words Save the code and generate new classes. Direct use of ASM is discouraged, because it requires you to be familiar with the internal structure of the JVM, including the format and instruction set of the class file.

Difference:
JDK's dynamic proxy (that is, the dynamic proxy of method one) has a limitation, that is, the object that uses the dynamic proxy must implement one or more interfaces . If you want to proxy a class that does not implement the interface , you can use Cglib to implement it.

JDK dynamic proxy is based on the interface . In other words, the proxy class and the target class implement the same interface. Then the method names of the proxy class and the target class are the same. CGLib dynamic proxy is the proxy class to inherit the target class . Then rewrite the method of the target class, so as to ensure that the proxy class has the same name method of the target class;

In Spring's AOP programming (summary):
If the target object added to the container has an implementation interface, use the JDK proxy.

If the target object does not implement the interface, use the Cglib proxy.

Guess you like

Origin blog.csdn.net/Hambur_/article/details/112755562