Spring boot uses AOP to handle logs uniformly

AOP I think everyone knows that sometimes we need to process some request logs or monitor some methods. What should we do if there are exceptions? Now, we introduce AOP from spring boot

[development environment: JDK version 1.8, spring boot version 1.4.1] {style = "background color: #ff0000"}

First, let's introduce the jar package,

Add the following to the POM file:

After introducing the jar package, we add two simple request processing methods to the boot method:

After adding two simple methods to handle requests, let's add our AOP

After the two are configured, next, we request and then view the print log. First, start our container, and then we first request the method of processing the request with parameters. The print log is as follows:

{width=”1232” height=”132”}

It can be found that the requested URL, method, args parameter value, type, and returned content are printed out, indicating that AOP interception is successful

Next, we will test the processing method of request without parameters. The print log is as follows:

{width=”1100” height=”130”}

We can find that the method parameter printed by this method is an empty array because the method does not need parameter passing

The above is how springboot refers to AOP for Web log processing. Next, we will add several main annotations on AOP aspects. The following is only the description and use of annotations. The author will not test them for specific tests. If you are interested, you can test them yourself:

Class annotation:

@Aspect defines a class as an aspect class @ order (I) marks the processing priority of the aspect class. The smaller the I value, the higher the priority PS: you can annotate classes or methods

Method notes:

@Pointcut defines a method as the pointcut, and the content in it is an expression. The following details @ before executing the method before the pointcut. The content is the specified pointcut @ after executing after the pointcut and before return, @ afterreturning executing after the pointcut and after return. If you want to process the return parameters of some methods, you can operate @ around the pointcut here. Before entering the pointcut, Execute @ afterthrowing after the pointcut. Throw an exception after the pointcut for processing @ order (I) mark the priority of the pointcut. The smaller I is, the higher the priority is

@Pointcut annotation combination:

In the above code, we define a pointcut that only processes the specified path:

Now, we are defining a tangent point to handle other paths:

The above pointcuts deal with different contents respectively. If we need a pointcut to deal with both of them, we can configure it as follows:

In the @ pointcut annotation, directly reference other method names annotated by @ pointcut, so that the pointcut can handle methods under two paths

@Execution expression in pointcut annotation: public * com demo.*.* (..)

The first public represents the modifier of the method. You can use * instead of the first * to represent the return value, and * represents all com demo.* Package path* Represents the third of all packages in the path* Represents the methods (..) of all classes under all packages under the path Represents an unlimited method parameter

Some notes on @ order (I) annotation:

Annotation class: the value of I is. The smaller the value, the higher the priority. Annotation methods are annotated in two cases: @ before is. The smaller the value of I is, the higher the priority is. Annotation methods are annotated in @ after or @ afterreturning. The larger the value of I is, the higher the priority is

The summary of the two is: the operations before the pointcut are executed from small to large according to the value of order, and the operations after the pointcut are executed from large to small according to the value of order

Extension:

Some watchers may ask, if I want to print the time required from the beginning to the end of the request, define a member variable to count the time, and access @ before and @ afterreturning at the same time, there may be synchronization problems, so we refer to a ThreadLocal to specify a generic object. The time of the request is recorded in @ before, and the time of the record is deducted in @ afterreturning, which is the consumed time, The code is as follows:

All the above are the results of my test. There may be discrepancies or errors. Please correct them

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>