Spring 中的切点表达式介绍

时间:2023-01-10 12:05:32

Spring 中的切点表达式介绍

翻译原文链接 Introduction to Pointcut Expressions in Spring

1. 概述

在本教程中,我们将讨论 Spring AOP 切点表达式语言。

In this tutorial we will discuss the Spring AOP pointcut expression language.

我们将首先介绍一些在面向切面编程中使用的术语。连接点是程序执行的一个步骤,例如方法的执行或异常的处理。在 Spring AOP 中,一个连接点总是代表一个方法的执行。切点是匹配连接点的谓词,切点表达式语言 是一种以编程方式描述切点的方式。

We will first introduce some terminology used in aspect-oriented programming. A join point is a step of the program execution, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution. A pointcut is a predicate that matches the join points and a pointcut expression language is a way of describing pointcuts programmatically.

2. 用法

切点表达式可以作为 @Pointcut 注解的值出现:

A pointcut expression can appear as a value of the @Pointcut annotation:

@Pointcut("within(@org.springframework.stereotype.Repository *)")
public void repositoryClassMethods() {}

方法声明称为切点签名。它提供了一个名称,通知注解可以使用该名称来引用该切点。

The method declaration is called the pointcut signature. It provides a name that can be used by advice annotations to refer to that pointcut.

@Around("repositoryClassMethods()")
public Object measureMethodExecutionTime(ProceedingJoinPoint pjp) throws Throwable {
    // ...
}

切点表达式也可以作为 aop:pointcut 标签的表达式属性的值出现:

A pointcut expression could also appear as the value of the expression property of an aop:pointcut tag:

<aop:config>
    <aop:pointcut  
         expression="@target(org.springframework.stereotype.Repository)"/>
</aop:config>

3. 切点指示符

切点表达式以切点指示符 (PCD) 开头,它是告诉 Spring AOP 匹配什么的关键字。有几个切点指示符,例如方法的 执行类型方法参数注解

A pointcut expression starts with a pointcut designator (PCD), which is a keyword telling Spring AOP what to match. There are several pointcut designators, such as the execution of a method, a type, method arguments, or annotations.

3.1 execution

Spring 中主要的切点指示符是 execution,它匹配方法执行连接点。

The primary Spring PCD is execution, which matches method execution join points.

@Pointcut("execution(public String com.baeldung.pointcutadvice.dao.FooDao.findById(Long))")

此示例切点将与 FooDao 类的 findById 方法的执行完全匹配。这(虽然)有效,但不是很灵活。假设我们想要匹配 FooDao 类的所有方法,这些方法可能具有不同的 签名返回类型参数。为了实现这一点,我们可以使用通配符:

This example pointcut will match exactly the execution of findById method of the FooDao class. This works, but it is not very flexible. Suppose we would like to match all the methods of the FooDao class, which may have different signatures, return types, and arguments. To achieve this we may use wildcards:

@Pointcut("execution(* com.baeldung.pointcutadvice.dao.FooDao.*(..))")

这里第一个通配符匹配任何返回值,第二个匹配任何方法名称,(..) 模式匹配任意数量的参数(零个或多个)。

Here the first wildcard matches any return value, the second matches any method name, and the (..) pattern matches any number of parameters (zero or more).

3.2 within

获得与上一节相同结果的另一种方法是使用 within 切点知识符,它将匹配限制为某些类型的连接点。

Another way to achieve the same result from the previous section is by using the within PCD, which limits matching to join points of certain types.

@Pointcut("within(com.baeldung.pointcutadvice.dao.FooDao)")

我们还可以匹配 com.baeldung 包或子包中的任何类型。

We could also match any type within the com.baeldung package or a sub-package.

@Pointcut("within(com.baeldung..*)")

3.3 this 和 target

this 将匹配限制为 bean 引用是给定类型的实例的连接点,而 target 将匹配限制为目标对象是给定类型的实例的连接点。前者在 Spring AOP 创建基于 CGLIB 的代理时工作,后者在创建基于 JDK 的代理时使用。假设目标类实现了一个接口:

this limits matching to join points where the bean reference is an instance of the given type, while target limits matching to join points where the target object is an instance of the given type. The former works when Spring AOP creates a CGLIB-based proxy, and the latter is used when a JDK-based proxy is created. Suppose that the target class implements an interface:

public class FooDao implements BarDao {
    // ...
}

在这个案例里,Spring AOP 将使用基于 JDK 的代理,您应该使用 target 切点指示符,因为代理对象将是 Proxy 类的实例并实现 BarDao 接口:

In this case, Spring AOP will use the JDK-based proxy and you should use the target PCD because the proxied object will be an instance of Proxy class and implement the BarDao interface:

@Pointcut("target(com.baeldung.pointcutadvice.dao.BarDao)")

反过来说,如果 FooDao 未实现任何接口或 proxyTargetClass 属性设置为 true,则代理对象将是 FooDao 的子类,并且可以使用 this 切点指示符:

On the other hand if FooDao doesn't implement any interface or proxyTargetClass property is set to true then the proxied object will be a subclass of FooDao and the this PCD could be used:

@Pointcut("this(com.baeldung.pointcutadvice.dao.FooDao)")

3.4 args

这个切点指示符用于匹配特定的方法参数:

This PCD is used for matching particular method arguments:

@Pointcut("execution(* *..find*(Long))")

这个切点匹配任何以 find 开头并且只有一个 Long 类型参数的方法。如果我们想匹配具有任意数量参数但第一个参数为 Long 类型的方法,我们可以使用以下表达式:

This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters but having the fist parameter of type Long, we could use the following expression:

@Pointcut("execution(* *..find*(Long,..))")

3.5 @target

这个 @target 切点指示符(不要与上述 target 指示符 混淆)将匹配限制为执行对象的类具有给定类型注释的连接点:

The @target PCD (not to be confused with the target PCD described above) limits matching to join points where the class of the executing object has an annotation of the given type:

@Pointcut("@target(org.springframework.stereotype.Repository)")

3.6 @args

此 切点指示符 将匹配限制为连接点,其中传递的实际参数的运行时类型具有给定类型的注解。假设我们要跟踪所有接受用 @Entity 注解的 bean 的方法:

This PCD limits matching to join points where the runtime type of the actual arguments passed have annotations of the given type(s). Suppose that we want to trace all the methods accepting beans annotated with @Entity annotation:

@Pointcut("@args(com.baeldung.pointcutadvice.annotations.Entity)")
public void methodsAcceptingEntities() {}

要访问参数,我们应该为 通知 提供一个 连接点 参数:

To access the argument we should provide a JoinPoint argument to the advice:

@Before("methodsAcceptingEntities()")
public void logMethodAcceptionEntityAnnotatedBean(JoinPoint jp) {
    logger.info("Accepting beans with @Entity annotation: " + jp.getArgs()[0]);
}

3.7 @within

此 切点指示符 将匹配限制为具有给定注解的类型中的连接点:

This PCD limits matching to join points within types that have the given annotation:

@Pointcut("@within(org.springframework.stereotype.Repository)")

这相当于:

Which is equivalent to:

@Pointcut("within(@org.springframework.stereotype.Repository *)")

3.8 @annotation

此 切点指示符 将匹配限制为连接点的主题具有给定注解的连接点。例如,我们可以创建一个 @Loggable 注解:

This PCD limits matching to join points where the subject of the join point has the given annotation. For example we may create a @Loggable annotation:

@Pointcut("@annotation(com.baeldung.pointcutadvice.annotations.Loggable)")
public void loggableMethods() {}

然后我们可以记录由该注解标记的方法的执行:

Then we may log execution of the methods marked by that annotation:

@Before("loggableMethods()")
public void logMethod(JoinPoint jp) {
    String methodName = jp.getSignature().getName();
    logger.info("Executing method: " + methodName);
}

4. 结合切点表达式

切入点表达式可以使用 &&|| 操作符进行组合:

Pointcut expressions can be combined using &&, || and ! operators:

@Pointcut("@target(org.springframework.stereotype.Repository)")
public void repositoryMethods() {}

@Pointcut("execution(* *..create*(Long,..))")
public void firstLongParamMethods() {}

@Pointcut("repositoryMethods() && firstLongParamMethods()")
public void entityCreationMethods() {}

5. 总结

Spring AOP切点 的 快速介绍中,我们展示了一些切点表达式的使用示例。

In this quick intro to Spring AOP and pointcuts, we illustrated some examples of pointcut expressions usage.

(本篇)完整的示例集 可以在 GitHub 上找到。

The full set of examples can be found over on GitHub.