Spring's five notification types

1. Pre-notification: Notification that executes execution before the target method is executed

       The pre-notification method can either have no parameters or receive an additional JoinPoint. Spring will automatically pass in the object to represent the current join point, through which information about the target object and the target method can be obtained.

Note that if you receive JoinPoint, you must ensure that it is the first parameter of the method, otherwise an error will be reported.

 2. Surround notifications: notifications that additional code can be executed both before and after the target method is executed

        The target method must be called manually in the surround advice, otherwise the target method will not be executed. This manual call is implemented through ProceedingJoinPoint, which can receive a parameter of this type in the surrounding notification, and the spring container will automatically pass in the object. This parameter must be in the first parameter position of the surrounding notification.

ProceedingJoinPoint is a subclass of JoinPoint. It should be noted that only surround notifications can receive ProceedingJoinPoint, while other notifications can only receive JoinPoint.

Configuration method: Surround notification needs to manually return the return value, otherwise the real caller will not get the return value, only a null.

Surround advice includes: controlling whether the target method is executed; executing additional code before or after the target method is executed; controlling whether to return a return value; the ability to change the return value, although surround advice has such capabilities, it must be used with caution and be careful not to destroy It achieves the goal of "high cohesion and low coupling" of software layering.

 

3. Post-notification: a notification that is executed after the target has successfully executed.

        Optionally receive a JoinPoint in the post-notification to get additional information about the join point, but this parameter must be the first in the parameter list.

 4. Exception notification: notification executed when the target method throws an exception.

         You can configure incoming JoinPoint to obtain information about the target object and target method, but it must be the first in the parameter list. In addition, you can also configure parameters to let the exception notification receive the exception object thrown by the target method

 5. Final notification: A notification that is executed after the target method is executed.

The difference from post-advice is that post-advice is a notification that is executed after the method returns normally. If the method does not return normally, such as throwing an exception, post-advice will not be executed. And the final notification will be executed after the target method is called anyway, even if the target method does not complete the normal execution.

In addition, the post notification can get the return value through configuration, but the final notification cannot.

The final notification can also receive an additional JoinPoint parameter to obtain information about the target object and the target method, but it must be the first parameter.

The order in which the five notifications are executed

a. In the case that the target method does not throw an exception:

advance notice

Surround notification (code before the target method is called by the surround notification, the target method, and the code after the target method is called by the surround notification)

post notification

final notice

b. In the case where the target method throws an exception:

advance notice

Surround notification (the code before the surround notification calls the target method, the target method throws an exception)

exception notification

final notice

c. If there are multiple facets:

For multi-faceted execution, the Chain of Responsibility design pattern is adopted.

The configuration order of the aspects determines the execution order of the aspects. The process of executing multiple aspects is similar to the process of method invocation. When the proceed() of the surrounding advice is executed, the next aspect is executed or if there is no next aspect, the target method is executed.

Five common usage scenarios for notifications

Pre-Notification: Logging (method is called)

Surround notifications: control transactions, permission control

Post notification: log (method was called successfully)

Exception notification: exception handling, control transactions

Final notification: log (method called, but not necessarily successful)

Guess you like

Origin blog.csdn.net/inexaustible/article/details/124381407