Quote:

It doesn’t matter that I’m currently optimizing the code, considering that logging operations are being done using components packaged by colleagues. What matters is that I’m going to use Spring AOP to do this today.

Environment Description:

This article is based on maven3.3.9+ Springboot 2.3.0.release + JDK 1.8.0_191

Implementation steps:

1. Define this class as an Aspect @aspect and auto-assemble it as a Component @Component.
2. Define @pointcut

Supported Pointcut Designators are Supported Pointcut Designators, execution, within, this, @target, @annotation, etc. This article will talk about the use of @annotation and execution, respectively.

2.1 @annotation is used flexibly so that we can cut wherever we want. First we define custom annotations for the method description at run time.

Usage: Use the custom annotation on the method you want to log to, either in the DAO layer or in the Services layer. Examples are annotated on the DAO layer method.

Execution (public * (..)) All methods beginning with set: Execution (set*(..). )

If this basic expression does not meet your needs, we can also use Combining Pointcut Expressions. The official explanation: You can combine pointcut expressions by using &&, | | and!

3. Notification Settings

Basically, what you want to do after you cut the way you want to cut, like log. There are several types of notification Settings, and this article covers pre – and post-only. Preposition is what you do before you cut, and postposition is what you do after you cut. In the case of logging, we can get the request method and request parameters by using the JoinPoint parameter.

conclusion

The examples and ideas written in the article are just the tip of the iceberg or an example of AOP, the actual needs or flexible use, I hope this article is helpful to every student who is learning AOP