As developers know, programming languages can be checked into dynamic and static types from run-time to compile-time. For Java, it is generally considered a static, strongly typed language, but because Java provides a mechanism for reflection, it also has the ability to partially dynamically type languages. In this section, we'll talk about dynamic proxies for Java.
Dynamic Proxy is a mechanism for dynamically building proxies at runtime and for dynamically processing proxy method calls. It is first of all a mechanism. In design mode, there is a pattern called proxy pattern, which is similar to a dynamic proxy, where the agent can be regarded as a wrapper over the target of the call, and the call to the target object is completed by the agent. This is actually a decoupling.
There are many mechanisms for implementing dynamic proxies, such as the dynamic proxy JDK proxy provided by the JDK itself (using the reflection mechanism of the Java language), and other methods are asm,cglib< based on Asm>,javassit. The dynamic agent mechanism is mainly applied to facet AOP, interceptors, RPC calls, logs, transactions, and so on.
Let's explore the JDK proxy mechanism < based on reflection >----through the interface. The new version also starts with the ASM mechanism. It involves the most important classes and interfaces are proxies and invocationhandler. Familiarize yourself with them first.
Proxy This class is used to dynamically create a proxy object class, its most commonly used method is newproxyinstance (ClassLoader loader, class[] interfaces, Invocationhandler h)
The Invocationhandler interface has only one method, and each dynamic proxy class must implement this interface.
With the initial impression, let's take a look at a simple example of a JDK proxy.
Cglib mechanism for generating dynamic proxies < inheritance by class based on asm>---:
By generating a subclass of a class as a proxy object, it can extend Java classes and implement Java interfaces at run time.
Cglib The dynamic proxy class pattern for creating a Class A is:
1. Find the method definition for all non-final public types on A;
2. Convert the definitions of these methods into byte codes;
3. Convert the composed byte code into the corresponding proxy class object;
4. Implement the Methodinterceptor interface to handle requests for all methods on the proxy class (this interface is the same as the capabilities and roles of the JDK Dynamic agent Invocationhandler)
By the way, spring AOP uses different techniques to create proxies at run time: JDK Proxy and Cglib.
Its mechanism is that if the target class implements one or more interfaces, spring will create a JDK dynamic proxy that implements each interface. If the target class does not implement an interface, Spring uses Cglib to dynamically create a new class, which is the subclass of the target ("extends"). This leads to an important difference: The JDK proxy cannot be converted to the original target class because it is just a dynamic proxy that implements exactly the same interface as the target. And cglib can be the same as the target class itself.
Advantages of JDK Proxy:
minimization of dependencies;
Smooth JDK version upgrades, and bytecode class libraries often need to be updated to ensure new versions of Java are used;
Code implementation is simple.
Benefits based on the CGLIB framework:
Sometimes the invocation of the target may be inconvenient to implement the additional interface, thus extending the Java class and implementing Java interface at runtime, from a certain point of view, the qualification of the caller implementation interface is some intrusive practice, similar to the Cglib dynamic agent does not have this limitation;
Operate only the classes we care about, not the other related classes;
Performance.
Welcome to pay attention to my public number
The principle of Java dynamic agent