1. Connection Point (joinpoint)
A specific position of the program execution, such as before the class starts initialization, after the class initialization, before a method of the class is called, after the call, and after the method throws an exception. Specific points in these codes are called "connection points ". Spring only supports the connection points of methods, that is, it can only enhance the implementation of these programs before and after a method call, when a method throws an exception, and before and after a method call.
2. pointcut)
Each program class has multiple connection points. For example, a class with two methods is a connection point. But among the numerous connection points, how can we locate an interesting connection point? AOP uses a "cut point" to locate a specific connection point. The concept of database query is used to understand the relationship between the cut point and the connection point: the join point is equivalent to the record in the database, and the cut point is equivalent to the query condition. A shard can match multiple connection points.
In spring, the cut point is described through the pointcut interface.
3. Advice)
Enhancement refers to a piece of program code woven into the target class connection point. The enhancement includes an execution logic used to add an object to the target connection point and information used to locate the object of the connection point. Therefore, the enhancement interfaces provided by spring contain the orientation Name: beforeadvice, afterreturningadvice, and throwsadvice.
4. Target object)
The enhancement logic is woven into the target class.
5. Weaving)
Weaving is the process of adding enhancements to the specific connection points of the target class. There are three ways to import AOP:
1) compile-time weaving, which requires a special Java compiler;
2) woven during the class loading period, which requires the use of special class loaders;
3) the dynamic proxy is woven to add enhanced subclass generation methods for the target class at runtime.
Spring uses dynamic proxy weaving, while aspectj uses compiling and class loading.
6. aspect)
A cut plane consists of a cut point and an enhancement. It includes both the definition of the cross-cutting logic and the definition of the connection point. Spring AOP is the framework responsible for implementing the cut plane, it organizes the cross-cutting logic defined by the cut plane into the connection point specified by the cut plane.
The focus of AOP mainly includes:
1) how to locate the connection point through the cut point and enhancement;
2) how to write the aspect code in the enhancement.
7. other basic knowledge
Spring AOP uses two proxy mechanisms: JDK-based Dynamic proxy and cglib-based Dynamic proxy. Two proxy mechanisms are required, largely because JDK only provides interface proxies, rather than class proxies.
JDK's dynamic proxy mainly involves two classes in the Java. Lang. Reflect package: proxy and invocationhandler.
Cglib uses a very low-level bytecode technology. It can create subclasses for a class and intercept calls of all parent classes by using methods in the subclass.
Studies have shown that the performance of the dynamic proxy object created by cglib is much higher than that of the proxy object created by JDK. However, cglib takes more time to create proxy objects than JDK dynamic proxies. Therefore, for Singleton proxy objects or proxies with instance pools, you do not need to frequently create proxy objects, therefore, cglib dynamic proxy technology is more suitable, while JDK dynamic proxy technology is more suitable. Cglib cannot proxy final methods in the target class.