Second, the five notifications of Spring AOP

One, five notification types:

Notice: When to execute the aspect method

Daily development is not commonly used, but interviews and written examinations will often be asked
Five notification types

  • After Returning Advice and After Throwing Advice are mutually exclusive

  • Before Advice and After Advice: The effect is similar to the finally block in try–catch–finally, it will be executed anyway

  • To understand the first 4 types, just know the timing of triggering. Around Advice should focus on mastering.
    Insert picture description here
    Introduction enhancement: just understand

Two, give an example:

1. After Advice:

Disadvantages: unable to obtain the return value generated during the operation of the target method or the exception thrown internally

use:

  1. Definition method:
    Insert picture description here
  2. Placement xml
    Insert picture description here
  3. Call (omitted)

2. After returning Advice

  • The biggest difference with post-notification: you can accept the return value of the target method

  • The execution order of post-notification and post-return notification is determined by the writing order in xml, and there is no default execution order

  • Example

  1. Definition method: There are 2 return values
    Insert picture description here
  2. Placement xml
    Insert picture description here
  3. Call (omitted)

3. Exception notification (After Throwing Advice)

  • Same as above, the order of log generation is in the order of configuration
  1. Definition method: (with 2 parameters)
    Insert picture description here

  2. xml file for configuration
    3.

  3. Trigger exception notification
    Insert picture description here

  4. Run (omitted)

4. Surround notification (emphasis!!!)

  • The most powerful notification, recommended

  • Note: The method of surrounding notification needs to add the return value Object (the return value is the return value of the method that triggered the surrounding notification)

  • The proceed() method -> ProceedingJoinPoin parameter calls this method, the target method will be executed, otherwise the target method will never be executed (the core method of controlling the target method), (the return value is the return value of the target method)

  • The reason for handling the exception here:
    Usually, multiple notifications may be laid out in a program. If we catch and handle the exception here, the subsequent notification program cannot catch this exception, and unexpected problems may occur.

  1. Configuration method
    Insert picture description here
  2. Configuration xml file
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/113704672