Detailed explanation of AOP annotation development example in spring

1、 Introduction

AOP mainly includes terms such as notification, pointcut and connection point, which are introduced as follows:

Advice

The notification defines what the aspect is and when to call, and when to call includes the following

Pointcut

The notification defines what and when the tangent plane is defined, where the tangent point is defined, and the definition of the tangent point will match one or more connection points to be woven into the notification. We usually use explicit class method names to specify these tangent points, or use regular expressions to define matching class and method names to specify these tangent points.

Join point

A connection point is a point that can be inserted into the aspect during application execution. This point can be used by the aspect code to insert these connection points into the normal process of the application and add new behaviors, such as log, security, transaction, cache, etc. when calling a method, throwing an exception, or even modifying a field.

2、 Annotation development

To declare an aspect, you only need to add the @ aspect attribute to the class name. The specific connection points are marked with @ pointcut, @ before, @ after, etc. Before declaring, we need to rely on the POM configuration

example

Synchronization in AOP

In the weblogaspect aspect, the pointcut header and the content executed after the pointcut return are realized through two independent functions dobefore and doafterreturning respectively. If we want to count the processing time of the request, we need to record the time at dobefore, and calculate the consumption time of the request processing at doafterreturning through the current time and the time recorded at the beginning.

So can we define a member variable in the weblogaspect aspect to access dobefore and doafterreturning together? Will there be synchronization problems?

Indeed, defining basic types directly here will have synchronization problems, so we can introduce ThreadLocal objects and record them as follows:

Priority of AOP section

Due to the implementation of AOP, the program has been well decoupled, but it will also bring some problems. For example, we may do multiple aspects of the web layer, verify users, verify header information, etc. at this time, we often encounter the processing sequence of aspects.

Therefore, we need to define the priority of each aspect, and we need the @ order (I) annotation to identify the priority of each aspect. The smaller the value of I, the higher the priority. Suppose we have another aspect where checknameaspect is used to verify that the name must be Didi, we set @ order (10) for it, and weblogaspect in the above is set to @ order (5), so weblogaspect has a higher priority. At this time, the execution order is as follows:

In @ before, first execute the contents of @ order (5), and then execute the contents of @ order (10)

In @ after and @ afterreturning, first execute the contents of @ order (10), and then execute the contents of @ order (5)

So we can summarize it like this:

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message.

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
分享
二维码
< <上一篇
下一篇>>