Spring's AOP understanding:

Spring's AOP understanding:

OOP is object-oriented, allowing developers to define vertical relationships, but it is not suitable for defining horizontal relationships, resulting in a large amount of code duplication, which is not conducive to the reuse of various modules.
AOP, generally called aspect-oriented, as a supplement to object-oriented , is used to extract and encapsulate common behaviors and logic that have nothing to do with the business but affect multiple objects into a reusable module, this module Named "Aspect", it reduces
the repetitive code in the system, reduces the coupling between modules, and improves the maintainability of the system. It can be used for authority authentication, log, and transaction processing.
The key to the realization of AOP lies in the proxy mode. AOP proxy is mainly divided into static proxy and dynamic proxy. The representative of static proxy is AspectJ; the representative of dynamic proxy is Spring AOP.
(1) AspectJ is an enhancement of static proxy . The so-called static proxy means that the AOP framework will generate AOP proxy classes during the compilation phase, so it is also called compile-time enhancement. It will weave AspectJ (aspects) into Java bytes during the compilation phase In the code, it is the enhanced
AOP object when it runs .
(2) The dynamic proxy used by Spring AOP . The so-called dynamic proxy means that the AOP framework does not modify the bytecode, but
temporarily generates an AOP object for the method in memory each time it runs . This AOP object contains the target object. All the methods of, and
enhanced processing at specific points of cut , and callback methods of the original object.
There are two main ways of dynamic proxy in Spring AOP, JDK dynamic proxy and CGLIB dynamic proxy:
①JDK dynamic proxy only provides interface proxy, and does not support class proxy. The core InvocationHandler interface and Proxy class,
InvocationHandler invokes the code in the target class through the invoke() method reflection, dynamically weaving the cross-cutting logic and business together
; then, Proxy uses InvocationHandler to dynamically create an instance that conforms to a certain interface to generate a proxy pair of the target class
Elephant.
② If the proxy class does not implement the InvocationHandler interface, then Spring AOP will choose to use CGLIB to dynamically proxy the
target class. CGLIB (Code Generation Library) is a class library for code generation. It can dynamically generate
a subclass object of a specified class at runtime , overwrite specific methods and add enhanced code to achieve AOP. CGLIB is a dynamic proxy through inheritance
, so if a class is marked as final, it cannot use CGLIB as a dynamic proxy.
(3) The difference between static proxy and dynamic proxy is that the timing of generating AOP proxy objects is different. Relatively speaking, the static proxy method of AspectJ has better performance, but AspectJ requires a specific compiler for processing, while Spring AOP does not require specific compilation器处理。 Processing.

Guess you like

Origin blog.csdn.net/m0_51684972/article/details/109227720