AOP of Spring Framework

Application scenarios of Spring AOP:

AOP provides a lot of convenience for the use of IOC. On the one hand, applications can directly use the functions of AOP, design the cross-cutting concerns of the application, abstract the functions of multiple modules across the application, and use simple AOP to flexibly compile To the module, for example, the log function in the application can be implemented through AOP. On the other hand, within Spring, some support modules are also implemented through Spring AOP, such as transaction processing.

The core technology of Spring AOP is dynamic proxy:

Dynamic proxy mode:

 

                                            static class diagram

subject is the object access interface. Both the RealSubject actual object and the Proxy proxy object implement this interface, so when the client accesses the actual object, it will be intercepted by the proxy object first, and additional processing actions will be added.
 

 

                                         Proxy timing diagram

Based on dynamic proxy, Spring has designed a series of AOP cross-cutting implementations, such as pre-notification, return notification, exception notification, etc.

For applications using Spring AOP, both ProxyFactoryBean and ProxyFactory provide AOP function encapsulation, but using ProxyFactoryBean can complete declarative configuration in the IOC container, while using ProxyFactory requires programmatic use of Spring AOP functions.

Implementation of AOP: 1. Create a proxy object for the target 2. Start the interceptor of the proxy object to complete the weaving of various cross-sections

JDK1.3 and above implement the feature of dynamic proxy, that is, to create proxy objects for any java object. But at the same time, the establishment of proxy objects can also be achieved through the third-party class generator CGLIB. Generally speaking, the default method is to use JDK to generate AopProxy proxy objects, but if the configuration target is not the implementation of the interface class, CGLIB will be used to generate the proxy object. Generates an AopProxy proxy object.

1. JDK implements proxy objects

The feature of JDK's dynamic proxy is implemented through the Java Reflection API. There is a Proxy object in the java reflection package, which is similar to the Proxy object in the Proxy mode. When using, it is also necessary to design a callback method for the proxy object. The function of the callback method is to add the preOperation and postOperation methods as additional processing actions required by the proxy.

The InvocationHandler interface needs to be implemented in the JDK.

/*

*proxy proxy object instance

*method the method to be called

*args The arguments to call the method*/

public interface InvocationHandler {

         public Object invoke(Object proxy,Method method,Object[] args) throws Throwable;

}

When a specific Proxy object is generated by the Porxy.newInstance method, the InvocationHandler can be set to the parameter to associate the invoke method with the Proxy.

 

 2. CGLIB generates proxy objects

1. Configure the Enhancer object and generate a proxy object through the Enhancer object,

2. Set the callback callback of Enhancer, such as the invoke callback method of the Proxy object of JDK.

 

The process of creating an AopProxy proxy object

1. Configure ProxyFactoryBean

     1) Define the notifier to use,

     2) Define ProxyFactoryBean and define it as a Bean, which is the main class that encapsulates AOP functions.

    3) Define the target attribute, the target object

 2. ProxyFactoryBean generates AopProxy proxy object

     The AOP implementation of ProxyFactoryBean needs to rely on the Proxy feature provided by JDK or CGLIB.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944318&siteId=291194637