Spring Boot - AOP (Aspect Oriented) - Pointcut Expression

The pointcut indicator is used to indicate the purpose of the pointcut expression. In Spring AOP, there is currently only one connection point, the execution method. The AspectJ pointcut indicator supported by Spring AOP, the pointcut expression can use && , || , ! To combine pointcut expressions, you can also use type matching wildcards to match. Type wildcards are as follows:

Type match wildcard

illustrate

*

means match any number of characters. Example: java.*.String, which means to match the String type under any "first-level sub-package" under the java package; such as matching java.lang.String, but not matching java.lang.ss.String

..

Represents repetition of any number of characters, such as matching any number of subpackages in a type pattern; and matching any number of arguments in a method parameter pattern. Example: java..*, which means to match any type in the java package and any subpackage; such as matching java.lang.String, java.lang.annotation.Annotation

+

Can only be placed after a type pattern as a suffix, matching subtypes of the specified type;

   

Details are as follows:

  • execution: The connection point used to match method execution, the configuration pointcut example @Pointcut( " execution ( pointcut expression )") , the pointcut expression format is as follows: execution(annotation ? modifiers-pattern? ret-type-pattern declaring -type-pattern? name-pattern(param-pattern) throws-pattern?)

Pointcut Expression Example

illustrate

public * *(..)

of any public method

@org.lixue.EnableLogTrace public * *(..)

Any public methods annotated with the org.lixue.EnableLogTrace annotation

* org.lixue..LogTrace+.*()

Any parameterless methods of the LogTrace interface and subtypes of the org.lixue package and all subpackages

   

  • within: used to match method execution within the specified type; configuration pointcut example @Pointcut( " within ( pointcut expression )")

Pointcut Expression Example

illustrate

org.lixue.. *

Any method in the org.lixue package or all subpackages executes

org.lixue..AccountService

Any method of type AccountService under the org.lixue package or all subpackages

org.lixue..LogTrace+

Any method of the LogTrace type and subtypes under the org.lixue package or all subpackages

   

  • this: The execution method used to match the type of the current AOP proxy object; note that the type of the AOP proxy object matches, which may include the introduction of interfaces and type matching; note that the expression used in this must be a fully qualified name of the type, and wildcards are not supported . Example configuration pointcut: @Pointcut("this(org.lixue.LogTrace)")

Pointcut Expression Example

illustrate

org.lixue.LogTrace

The type of the AOP proxy object implements any method of the org.lixue.LogTrace interface

   

  • target: the execution method used to match the type of the current target object; note that the type of the target object matches, so that the imported interface is not included and the type matches; note that the expression used in target must be a fully qualified name of the type, and wildcards are not supported; configuration Example pointcut: @Pointcut("target(org.lixue.LogTrace)")

Pointcut Expression Example

illustrate

org.lixue.LogTrace

Any method that implements the org.lixue.LogTrace interface

   

  • args: used to match the currently executed method. The incoming parameter is the execution method of the specified type; the parameters in the parameter type list must be the fully qualified name of the type, and wildcards are not supported; args is a dynamic pointcut, which is very expensive. , it is best not to use it in non-special cases; note: match the incoming parameter type, not the parameter type that matches the method signature. , configure pointcut example @Pointcut( " args(java.lang.String,java.lang.String)")

Pointcut Expression Example

illustrate

args (java.io.Serializable,..)

Any method that begins with accepting "the incoming parameter type is java.io.Serializable" and can be followed by any number of parameters of any type executes

args(java.lang.String,java.lang.String)

Any one that accepts two parameters and is of type java.lang.String

   

  • @within: used to match all methods within the specified annotation type; configuration pointcut example @Pointcut( "@ within ( annotation type )") , the annotation type must also be a fully qualified type name

Pointcut Expression Example

illustrate

org.lixue.EnableLogTrace

Any method of any type annotated with org.lixue.EnableLogTrace must be declared on the target object, declared on the interface has no effect on it

   

  • @target: The execution method used to match the current target object type, where the target object holds the specified annotation; the configuration pointcut example @Pointcut("@target(annotation type)"), the annotation type must also be a fully qualified type name

Pointcut Expression Example

illustrate

org.lixue.EnableLogTrace

Any method of any type annotated with org.lixue.EnableLogTrace must be declared on the target object, declared on the interface has no effect on it

   

  • @args: used to match the execution of the specified annotation with the parameters passed in by the currently executed method;

Pointcut Expression Example

illustrate

org.lixue.EnableLogTrace

Any method of any type annotated with org.lixue.EnableLogTrace must be declared on the target object, declared on the interface has no effect on it

   

  • @annotation:用于匹配当前执行方法持有指定注解的方法,配置切入点示例 @Pointcut("@annotation(注解类型)"),注解类型也必须是全限定类型名

切入点表达式示例

说明

org.lixue.EnableLogTrace

使用 org.lixue.EnableLogTrace 注解的任何方法

   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325577662&siteId=291194637