[Spring Tutorial 20] Spring Framework Practical Combat: AOP (Aspect-Faced Programming) Knowledge Summary

Welcome back to "Java Tutorial: Spring 30-Day Quick Start". All examples in this tutorial are based on Maven. If you are interested in Maven You are still unfamiliar with it, please refer to my blog post "How to install and configure Maven under Windows 11 and configure the Maven environment with IDEA". The previous article of this article is "Using AOP notification to obtain data code example"

Insert image description here
Through Sections 11 to 14 of this abbreviation, the knowledge of AOP has been explained. Next, we will give a summary of the knowledge of AOP:

1 Core concepts of AOP

  • Concept: AOP (Aspect Oriented Programming), a programming paradigm
  • Function: Enhance the functionality of the method without disturbing the original design
  • Core idea
    • Proxy: The core essence of SpringAOP is implemented using the proxy mode
    • JoinPoint: In SpringAOP, it is understood as the execution of any method
    • Pointcut: a formula that matches connection points, and is also a method description with common functions.
    • Advice: The common function of several methods, executed at the entry point, and finally embodied as one method
    • Aspect: Describes the correspondence between notifications and entry points
    • Target: The original object being proxied becomes the target object

2 Pointcut expression

Standard format of pointcut expression: action keyword (access modifier return value package name. class/interface name. method name (parameter) exception name)

execution(* com.itheima.service.*Service.*(..))
  • Pointcut expression description wildcard:
    • Function: used for quick description, scope description
    • *: matches any symbol (commonly used)
    • …: Match multiple consecutive arbitrary symbols (commonly used)
    • +: Match subclass type
  • Tips for writing pointcut expressions
    • 1. Developed according to standard specifications
    • 2. It is recommended to use * matching for the return value of the query operation
    • 3. Reduce the use of… to describe packages
    • 4. Describe the interface and use to represent the module name. For example, the matching description of UserService is Service
    • 5. Write method names to retain verbs, such as get, and use * to represent nouns. For example, getById matching description isgetBy*
    • 6. Parameters can be flexibly adjusted according to actual conditions

3 Five Notification Types

  • Pre-notification
  • post notification
  • Surround notifications (emphasis)
    • Surrounding advice relies on the formal parameter ProceedingJoinPoint to implement the call to the original method.
    • Surround advice can isolate the execution of the original method call
    • The surround notification return value is set to the Object type
    • Surround advice can handle exceptions that occur during the original method call.
  • Notify after return
  • Notify after an exception is thrown

4 Get parameters in notification

  • Get the parameters of the entry point method. All notification types can get the parameters.

    • JoinPoint: Applicable to pre-, post-, post-return, and post-exception notifications
    • ProceedingJoinPoint: for surround notifications
  • Get the return value of the entry point method. There is no return value in the pre- and post-exception notifications. The post-notifications are dispensable, so we will not do any research.

    • Notify after return
    • surround notification
  • Obtain the entry point method running exception information. There will be no pre- and post-return notifications. Post-notifications are optional, so we will not do any research.

    • Notify after an exception is thrown
    • surround notification

    We have already explained the relevant knowledge of AOP. Starting from the next section, we start to explain the transaction management of AOP.

Guess you like

Origin blog.csdn.net/shenchengyv/article/details/134885867
Recommended