5.3 three key concepts of AOP
Because the concept of AOP is difficult to understand, I first explained the Java Dynamic proxy mechanism, so that readers can gradually understand the idea of AOP.
The key to learning AOP is to understand the idea of AOP and be able to use it. For many AOP concepts, you only need to understand three important concepts. The three concepts are pointcut, advice, and advisor.
5.3.1 pointcut)
Before introducing pointcut, it is necessary to introduce the concept of join point first. A join point is a stage point in the program running, such as a method call or exception throw. In the preceding example, the doauditing () method is a join point, indicating that the program wants to add advice to this place.
Pointcut is the set of join points. It is the set of locations where advice needs to be injected in the program and specifies the conditions under which advice can be triggered.
The Org. springframework. AOP. pointcut interface is used to specify the classes and methods for notification. View the source file pointcut. Java in the spring download package, the path is spring-framework-2.0-m1/src/org/springframework/AOP, You can see pointcut. java. source code is as follows:
// ******** Pointcut. Java **************
Package org. springframework. AOP;
Public interface pointcut {
// Used to limit the entry point to the specified target class
Classfilter getclassfilter ();
// Used to determine whether the entry point matches the method specified by the target class
Methodmatcher getmethodmatcher ();
Pointcut true = truepointcut. instance;
}
Code Description:
● The classfilter interface is used to restrict the entry point to a specified target class.
● The interface methodmatcher is used to determine whether the entry point matches the method specified by the target class.
As shown above, the pointcut interface mainly contains two interfaces: classfilter and methodmatcher, which are conducive to code reuse.
5.3.2 notification (advice)
Advice is the processing logic used by a connection point, that is, the code injected into the connection point. The Code extracted from the preceding example to output log information is an advice, indicating that this code should be added to the join point.
5.3.3 Advisor
Advisor is the configuration tool of pointcut and advice. It includes pointcut and advice. It is the code that injects advice into the pointcut position of the program.
The above is just a rough description of the three concepts of AOP, the purpose is to allow readers to quickly enter the AOP, next, we will explain these three concepts in more detail.
5.4 pointcut implementation
As mentioned in the previous section, pointcut is the set of join points, which is the set of locations where advice needs to be injected in the program. Spring provides three types of pointcut implementation: static, dynamic, and custom.
5.4.1 static entry point
The static entry point is limited to the given method and target class, without considering the parameters of the method. When spring calls a static entry point, it only calculates the position of the static entry point at the first time, and then caches it. It does not need to be computed in the future. Use Org. springframework. AOP. support. regexpmethodpointcut can realize static entry point, regexpmethodpointcut is a general Regular Expression entry point, which is achieved through Jakarta Oro, need to add the jakarta-oro-2.0.8.jar to classpath, its regular expression syntax is the same as that of Jakarta Oro. A sample code for using regexpmethodpointcut is as follows:
<Bean id = "settersandabsquatulatepointcut"
Class = "org. springframework. AOP. Support. regexpmethodpointcut">
<Property name = "patterns">
<! -- Set the entry point -->
<List>
<Value>. * Save. * </value>
<Value>. * do. * </value>
</List>
</Property>
</Bean>
Code Description:
●. * Save. * indicates that all methods starting with Save are the starting point.
●. * Do. * indicates that all methods starting with do are the starting point.
5.4.2 dynamic entry point
The difference between a dynamic entry point and a static entry point is that it not only limits the methods and classes of the points, but also specifies the parameters of the methods. Because of the variability of parameters, dynamic entry points cannot be cached and need to be calculated each time a call is made. Therefore, dynamic entry points have a high performance loss.
When the entry point needs to call the notification based on the parameter value during execution, a dynamic entry point is required. Spring provides a built-in dynamic entry point: the control flow entry point. This entry point matches the call stack based on the current thread. The developer returns true only when a specific class and method are found during execution of the current thread.
In fact, most of the starting points can use static starting points, so there is little chance to create dynamic starting points.
5.4.3 custom entry point
Because the entry point in spring is a Java class, rather than a language feature (such as aspectj), you can define a custom entry point. Because AOP is not yet fully mature, the documentation provided by spring does not provide more detailed explanations in this respect, so we will not detail dynamic and custom start points here.