Fast learning -Spring (AOP annotations configuration)

Chapter 5 AOP configuration notes

5.1 pointcut expression

5.1.1 Role

Locating one or more specific connection point by way of expression.

5.1.2 Syntax details

  1. Pointcut expression syntax
execution([权限修饰符] [返回值类型] [简单类名/全类名] [方法名]([参数列表]))
  1. for example
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
  2. In AspectJ, pointcut expression by "&&", "||", "!", Etc. combine operator.
    Here Insert Picture Description

5.1.3 entry point expression to the actual aspect classes

Here Insert Picture Description

5.2 current connection point details

5.2.1 Overview

Pointcut expression usually positioning method from a group of the macro, and a notification specific annotations can be combined to determine the corresponding connection point. So in terms of a specific connection point, we might care about this specific connection point of information, for example: The current method of connection point where the name of the method, the current value of the parameter passed and so on. This information is encapsulated in the object instance JoinPoint interface.

5.2.2 JoinPoint

Here Insert Picture Description

5.3 notification

5.3.1 Overview

  1. Operations on specific connection point to be performed.
  2. A section may include one or more notifications.
  3. Values ​​used to inform annotated expression is often the starting point.

5.3.2 Pre-notification

  1. Before advice: Advice to be executed before the execution method
  2. Use annotations @Before

5.3.3 Rear notice

  1. Post Notification: Rear notification is performed after the completion of the connection point, i.e. the point of attachment of the return or throw an exception when the result
  2. Use annotations @After

5.3.4 return notification

  1. Back to News: Whether the connection point is a normal return or an exception is thrown, after advice will be executed. If you want to log when the connection point is returned, return notification should be used instead of post-notification.
  2. Use @AfterReturning annotation, access point connection notification returns the return value
    ① in the return notification, as long as the property of returning to @AfterReturning added annotation, can access the return value of the connection points. The value of this attribute is the name of the parameters passed to the return value of
    ② must add a parameter of the same name in the signature notification method. Spring AOP at runtime return value passed through this parameter
    ③ original tangent point needs to appear in the expression property pointcut
    Here Insert Picture Description

5.3.5 exception notification

  1. Abnormal notice: if abnormal abnormality notification only thrown at the connection point
  2. 将throwing属性添加到@AfterThrowing注解中,也可以访问连接点抛出的异常。Throwable是所有错误和异常类的顶级父类,所以在异常通知方法可以捕获到任何错误和异常。
  3. 如果只对某种特殊的异常类型感兴趣,可以将参数声明为其他异常的参数类型。然后通知就只在抛出这个类型及其子类的异常时才被执行

5.3.6环绕通知

  1. 环绕通知是所有通知类型中功能最为强大的,能够全面地控制连接点,甚至可以控制是否执行连接点。
  2. 对于环绕通知来说,连接点的参数类型必须是ProceedingJoinPoint。它是 JoinPoint的子接口,允许控制何时执行,是否执行连接点。
  3. 在环绕通知中需要明确调用ProceedingJoinPoint的proceed()方法来执行被代理的方法。如果忘记这样做就会导致通知被执行了,但目标方法没有被执行。
  4. 注意:环绕通知的方法需要返回目标方法执行之后的结果,即调用 joinPoint.proceed();的返回值,否则会出现空指针异常。
    Here Insert Picture Description

5.4 重用切入点定义

  1. 在编写AspectJ切面时,可以直接在通知注解中书写切入点表达式。但同一个切点表达式可能会在多个通知中重复出现。
  2. 在AspectJ切面中,可以通过@Pointcut注解将一个切入点声明成简单的方法。切入点的方法体通常是空的,因为将切入点定义与应用程序逻辑混在一起是不合理的。
  3. 切入点方法的访问控制符同时也控制着这个切入点的可见性。如果切入点要在多个切面中共用,最好将它们集中在一个公共的类中。在这种情况下,它们必须被声明为public。在引入这个切入点时,必须将类名也包括在内。如果类没有与这个切面放在同一个包中,还必须包含包名。
  4. 其他通知可以通过方法名称引入该切入点
    Here Insert Picture Description

5.4 指定切面的优先级

  1. 在同一个连接点上应用不止一个切面时,除非明确指定,否则它们的优先级是不确定的。
  2. 切面的优先级可以通过实现Ordered接口或利用@Order注解指定。
  3. 实现Ordered接口,getOrder()方法的返回值越小,优先级越高。
  4. If you use @Order notes, numbers appear in a comment
    Here Insert Picture Description
    Here Insert Picture Description
Released 1333 original articles · won praise 1090 · Views 100,000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/104274706