[Spring AOP underlying implementation principle]

Spring AOP underlying implementation principle

The underlying implementation principle of Spring AOP is to dynamically generate proxy objects at runtime and implement method interception of the target object through the proxy chain.

During initialization, it will check whether the target class implements the InvocationHandler interface or the Proxy class. If the interface is implemented, the JDK dynamic proxy is used to receive the proxied class through reflection.

If it is not implemented, use cglib for AOP dynamic proxy. CGLIB is a dynamic proxy through inheritance. It is a code-generated class library that can dynamically generate a subclass of a certain class at runtime and convert the target object into a proxy. Objects operate on transactions. Therefore, during initialization, the target object has been proxied and put into the spring container.

CGLIB is a bytecode generation library developed based on ASM. It implements functions such as AOP and dynamic proxy by generating subclasses of the target class. ASM is an independent bytecode manipulation toolkit. The application scenarios of Spring's AOP are generally in business scenarios such as logging, permission verification, and transaction management.

It is woven during runtime, generates bytecode, and then loaded into the virtual machine.

Guess you like

Origin blog.csdn.net/java_wxid/article/details/132890118