AOP (Aspect Oriented Programming), which is aspect-oriented programming, is a traditional and maintained technology that realizes program functions through pre-compilation and dynamic proxy during runtime.
Function: During the running of the program, some methods can be functionally enhanced without modifying the source code
Advantages: Reduce duplicate code , improve development efficiency, and facilitate maintenance
jdk proxy: interface-based dynamic proxy technology
cglib Proxy: Dynamic proxy technology based on parent class
List item- Target (target object): proxy Target object
Proxy (proxy): After a class is woven into the enhancement by AOP, a resulting proxy class
Joinpoint (connection point ): Connection points are those points that are intercepted. In Spring, these points refer to methods, because Spring only supports method type connection points (methods that can be enhanced are called connection points)
PointCut (pointcut): pointcut It refers to the definition of which Joinpoints we want to intercept
Advice (notification/enhancement): Notification refers to what we need to do after intercepting the Joinpoint is to notify
Aspect (aspect): It is the combination of pointcuts and notifications
Weaving (weaving): The process of applying enhancements to the target object to create a new proxy object. Spring uses dynamic proxy weaving, while AspectJ uses compiler weaving and class loader weaving
I use annotation-based AOP development here .
Development steps
Add dependencies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Create the target interface and target class (with cut points inside)
Create aspect classes (with enhancement methods inside)
Leave the object creation rights of the target class and aspect class to Spirng
Use annotations to configure the weaving relationship in the aspect class
Enable component scanning and AOP in the configuration file Automatic proxy
#Because my project is a SpringBoot Web project, just enable annotations here.
The following code is the atomic counting class used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
The following code is the global interface monitoring implemented.
I simply use Redis for caching in the project, and you can see all Redis-related operations.
Use @Before to configure pre-notification. Specifies that the enhanced method is executed before the pointcut method.
Use @ @AfterReturning to configure post notification. Specifies that the enhanced method is executed after the pointcut method.
Use @ @AfterThrowing to configure exception throwing notifications. Specifies that the enhanced method is executed when an exception occurs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
The Controller layer code is given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
After the project has been running for a period of time, you can see the number of requests for the interface in Redis.
The final rendering of Web is as follows:
The above is the detailed content of How SpringBoot uses AOP to count the number of global interface visits. For more information, please follow other related articles on the PHP Chinese website!