What is the use of Spring-Aop?

What is the use of Spring-Aop?

If there is no aop, when doing log processing, we will add log processing to each method, such as

1

But most of the day processing code is the same, in order to achieve code reuse, we may separate the log processing into a new method. But then we still have to insert these methods manually.

2

But these two methods are strongly coupled. If we don't need this function at this time, or want to change to other functions, then we must modify them one by one.

Through the dynamic agent, the corresponding process can be executed at the specified location. In this way, some horizontal functions can be extracted to form an independent module, and then these functions can be inserted in the designated position. This kind of thinking is called aspect-oriented programming, or AOP.

3

In order to perform these horizontal functions at the specified position, you need to know where the specified is

4

For example, in the above figure, the method-level aop implementation, in a program execution chain, method2 is called the point of cut, that is to say, the crosscutting function will be executed when method2 is executed, then it is executed before or after method2 what? These are all specified by advice. There are 5 types of advice, namely

Notification type Introduction
Before (pre-notification) Execute before target method call
After (post notification) Execute after the target method is called
After-returning (return notification) Execute after successful execution of target method
After-throwing (abnormal notification) Execute after the target method throws an exception
Around (around notification) Equivalent to combining the front and rear

Combining pointcuts and notifications is an aspect. An aspect specifies when and where to execute which method. Define this aspect in spring aop as follows:


   
    
    
  1. @Aspect
  2. @Component
  3. public class UserAspect {
  4. @Before( "execution(* com.aop.service.impl.UserServiceImpl.login(..))")
  5. public void loginLog(){
  6. System .out .println( "user login");
  7. }
  8. }

Use the annotation @Aspect to declare a specific class as an aspect, so that the methods under the class can be declared as horizontal function points and inserted into the specified position. Use the execution expression to declare at this point of contact, the format is as follows 5

The first position specifies the return value of the method. The * sign represents the return value of any type, and then the class and method name where it is located. The * sign also represents any method in the class. In the previous example, the method name If it is login, it specifies the login method in this class. Then the last parameter is the method parameter, because java supports overloading, so this parameter can help you locate more accurately. Two points indicate any parameter type. In this way, the execution expression tells the program where to execute the notification. The method modified by annotations such as @Before is the content of the notification, that is, what to do.

At this point, we can use spring aop, but there are two points that need to be paid attention to

  1. Declare the aspect class as a bean
  2. The class where the method specified by the pointcut is located also needs to be injected by spring to take effect

Continuously updating...

Guess you like

Origin blog.csdn.net/weixin_44325444/article/details/106477118