Introduction to Pointcut Expressions in Spring – Spring中的指向性表达式介绍

最后修改: 2015年 12月 16日

1. Overview


In this tutorial, we’ll discuss the Spring AOP pointcut expression language.

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

First, we’ll 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 the pointcut expression language is a way of describing pointcuts programmatically.

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

2. Usage


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 advice annotations can use to refer to that pointcut:


public Object measureMethodExecutionTime(ProceedingJoinPoint pjp) throws Throwable {

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


    <aop:pointcut id="anyDaoMethod" 

3. Pointcut Designators


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, type, method arguments, or annotations.

一个切点表达式以切点指定器(PCD)开始,这是一个告诉Spring AOP要匹配什么的关键字。有几个点切代号,例如方法的执行、类型、方法参数或注释。

3.1. execution


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


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

This example pointcut will exactly match the execution of the findById method of the FooDao class. This works, but it’s not very flexible. Suppose we’d like to match all the methods of the FooDao class, which may have different signatures, return types, and arguments. To achieve this, we can 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


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

实现与上一节相同结果的另一种方法是使用within PCD,它将匹配限制在某些类型的连接点。


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



3.3. this and target


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:

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

public class FooDao implements BarDao {

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

在这种情况下,Spring AOP将使用基于JDK的代理,我们应该使用target PCD,因为被代理的对象将是Proxy类的实例并实现BarDao接口。


On the other hand, if FooDao doesn’t implement any interface, or the proxyTargetClass property is set to true, then the proxied object will be a subclass of FooDao and we can use the this PCD:



3.4. args


We can use this PCD for matching particular method arguments:


@Pointcut("execution(* *..find*(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 still having the fist parameter of type Long, we can use the following expression:


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

3.5. @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:

@target PCD(不要与上面描述的target PCD混淆)将匹配限制在执行对象的类具有给定类型的注释的连接点。


3.6. @args


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 the @Entity annotation:


public void methodsAcceptingEntities() {}

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


public void logMethodAcceptionEntityAnnotatedBean(JoinPoint jp) {"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:



Which is equivalent to:


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

3.8. @annotation


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


public void loggableMethods() {}

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


public void logMethod(JoinPoint jp) {
    String methodName = jp.getSignature().getName();"Executing method: " + methodName);

4. Combining Pointcut Expressions


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


public void repositoryMethods() {}

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

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

5. Conclusion


In this brief article about Spring AOP and poincuts, we illustrated some examples of pointcut expressions.

在这篇关于Spring AOP和poincuts的简短文章中,我们说明了一些pointcut表达式的例子。

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