Java proxy mechanism (static proxy and dynamic proxy)

I Concept of agent

Provide a proxy for an object to control access to the object.

Proxy classes and delegate classes have a common parent class or parent interface, so proxy objects can be used wherever delegate objects are used. The agent class is responsible for request preprocessing

Filtering, dispatching the request to the delegate class for processing, and subsequent processing after the delegate class executes the request.

As can be seen from the figure, the proxy interface (subject), proxy class (proxysubject) and delegate class (realsubject) form a "product" structure.

II Classification of agents

According to the generation time of agent classes, agents can be divided into static agents and dynamic agents.

Static proxy

The programmer creates or tool generates the source code of the agent class, and then compiles the agent class. The so-called static means that the bytecode file of the agent class already exists before the program runs, and the relationship between the agent class and the delegate class is determined before the program runs.

advantage:

The business class only needs to pay attention to the business logic itself, which ensures the reusability of the business class, which is the common advantage of the agent.

Disadvantages:

1. An interface of a proxy object only serves one type of object. If there are many methods to proxy, it is bound to proxy each method. When the program scale is slightly large, the static proxy is not competent.

2. If a method is added to the interface, all proxy classes need to implement this method in addition to all implementation classes. It increases the complexity of code maintenance.

Static proxy -- code implementation git path:

https://github.com/jichunyang19931023/dailyDemo/tree/master/staticProxyModel

Dynamic proxy (implementation principle of spring AOP)

Dynamic agent is created dynamically by using reflection mechanism when the program is running. Dynamic agent can be implemented in two ways: JDK dynamic agent and cglib dynamic agent.

JDK dynamic agent

When creating the proxy class bytecode of JDK dynamic proxy, you need to implement the interface implemented by the business implementation class as a parameter. If the business implementation class does not implement the interface but directly defines the business method, the JDK dynamic proxy cannot be used. (the important feature of JDK dynamic proxy is the proxy interface). Moreover, if the business implementation class adds methods that are not in the interface, these methods cannot be proxy (because they cannot be called).

Cglib dynamic proxy

For classes without interfaces, you need to use cglib to implement dynamic proxies. Cglib adopts the very low-level bytecode technology. Its principle is to create a subclass for a class through bytecode technology, and use the method interception technology to intercept the calls of all parent methods in the subclass, so as to weave cross cutting logic. JDK dynamic proxy and cglib dynamic proxy are the basis for implementing spring AOP.

JDK dynamic agent and cglib dynamic agent -- code implementation git path:

https://github.com/jichunyang19931023/dailyDemo/tree/master/dynamicProxyModel

Differences between JDK and cglib dynamic proxy methods

1, Java dynamic proxy uses reflection mechanism to generate an anonymous class that implements the proxy interface. It calls InvokeHandler before calling the specific method. Cglib dynamic proxy uses ASM open source package to load the class file of proxy object class and generate subclasses by modifying its bytecode.

2. JDK dynamic agent can only generate agents for classes that implement interfaces, not for classes; Cglib implements a proxy for a class. It mainly generates a subclass of a specified class and overrides the methods in it. Because it is inheritance, it is better not to declare this class or method as final.

3. Cglib a target class method will generate two proxy methods, one rewrites the target method and implements the proxy logic, and the other directly calls the target class method.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>