Abstract ： This article mainly talked about Spring Aop There are two ways to implement dynamic proxy .
1. Spring AOP
Spring It's a lightweight container ,Spring The core concept of the whole series is IoC、AOP. so AOP yes Spring One of the cores of the framework , It plays a very important role in application , It's also Spring The basis of other components .AOP（Aspect Oriented Programming）, That is, face-to-face programming , Can be said to be OOP（Object Oriented Programming, object-oriented programming ） To complement and perfect .OOP Imported packaging 、 Inherit 、 The concept of polymorphism is used to establish an object hierarchy , A collection used to simulate public behavior . however OOP Allows developers to define vertical relationships , But it's not appropriate to define horizontal relationships , For example, log function .
About AOP Basic knowledge of , Is not the point of this article , Let's mainly look at AOP The underlying implementation mechanism of the core functions of ： The principle of dynamic agent .AOP The interception function of is provided by java To implement the dynamic agent in . Add section logic to the target class , Generate enhanced target classes （ The facet logic is either before the target class function is executed , Or after the target class function is executed , Or when the target class function throws an exception . Different entry time corresponds to different Interceptor The type of , Such as BeforeAdviseInterceptor,AfterAdviseInterceptor as well as ThrowsAdviseInterceptor etc. ）.
So how does the dynamic agent implement the section logic （advise） Woven into the target class method ？ Now let's introduce and implement AOP Two dynamic proxies used in .
AOP Two kinds of dynamic agents are used in the source code of ：jdk Dynamic proxy sum cglib A dynamic proxy . Two methods exist at the same time , Each have advantages and disadvantages .jdk Dynamic agents are created by java Internal reflection mechanism ,cglib The bottom layer of dynamic agent is based on asm To achieve . in general , Reflection mechanism is more efficient in the process of class generation , and asm It is more efficient in the related execution process after the class is generated （ It can be done by putting asm Generated classes for caching , Solve it like this asm Low efficiency of generating class process ）.
Let's implement these two methods respectively .
2. JDK A dynamic proxy
2.1 Define interfaces and implementation classes
The above code defines an intercepted object interface , Crosscutting concerns . The following code implements the intercepted object interface .
2.2 JDK Dynamic proxy class
The above code implements the dynamic proxy class JDKProxy, Realization InvocationHandler Interface , And implement the invoke Method . When the client calls the business method of the proxy object , Proxy object execution invoke Method ,invoke Method delegate the call to targetObject, Equivalent to calling the method of the target object , stay invoke Determine permissions before method delegation , Implementation method interception .
give the result as follows ：
3. CGLIB Bytecode generation
3.1 Class to proxy
CGLIB You can generate a proxy for the class of the interface , You can also generate proxies for classes . Example , Implementing a proxy for a class .
The implementation of this class is the same as the interface implementation above , In order to maintain unity .
3.2 CGLIB Dynamic proxy class
The above implementation of the creation of subclasses and proxy methods .getProxy(SuperClass.class) Method through the input parameter, that is, the bytecode of the parent class , Extend the parent's class To create a proxy object .intercept() Method to intercept all calls to methods of the target class ,obj Represents an instance of the target class ,method Is the reflection object of the target class method ,args Is the dynamic input parameter of the method ,methodProxy For proxy class instances .method.invoke(targetObject, args) Calling methods in the parent class through the proxy class .
give the result as follows ：
This article mainly talked about Spring Aop There are two ways to implement dynamic proxy , The advantages and disadvantages are introduced .jdk The premise of dynamic proxy application is that the target class is based on a unified interface . Without that premise ,jdk Dynamic agent cannot be applied . From this we can see that ,jdk Dynamic agent has some limitations ,cglib This kind of third-party class library realizes the application of dynamic agent more widely , And more efficient .
JDK Dynamic agent mechanism is principal mechanism , No need for third-party libraries , As long as you want to JDK The environment can act as a proxy , Dynamic implementation interface class , In the dynamically generated implementation class, the delegate is hanlder To call the original implementation class method ;CGLib Must depend on CGLib Class library of , Using the inheritance mechanism , Is the relationship between the proxy class and the inheritance of the proxy class , So the proxy class can be assigned to the proxy class , If the proxy class has an interface , Then the proxy class can also be assigned to the interface .
- jdk Dynamic agent and cglib The principle of agency
- AOP The underlying implementation of -CGLIB Dynamic proxy sum JDK A dynamic proxy
This article is shared from Huawei cloud community 《 Also don't understand, Spring AOP？ This article takes you to understand dynamic agent 》, Original author ：aoho .