Declaring a Pointcut

切入点确定感兴趣的连接点,从而让我们能够控制何时运行建议。Spring AOP仅为Spring Bean支持方法执行连接点,因此您可以将切入点视为与Spring Bean上的方法执行相匹配。切入点声明有两部分:包含名称和任何参数的签名,以及切入点表达式,该表达式准确确定我们感兴趣的哪个方法执行。在@AspectJ注释风格的AOP中,切入点签名由常规方法定义提供,而切入点表达式通过使用`@Pointcut`注释表示(用作切入点签名的该方法必须具有`void`返回类型)。

Pointcuts determine join points of interest and thus enable us to control when advice runs. Spring AOP only supports method execution join points for Spring beans, so you can think of a pointcut as matching the execution of methods on Spring beans. A pointcut declaration has two parts: a signature comprising a name and any parameters and a pointcut expression that determines exactly which method executions we are interested in. In the @AspectJ annotation-style of AOP, a pointcut signature is provided by a regular method definition, and the pointcut expression is indicated by using the @Pointcut annotation (the method serving as the pointcut signature must have a void return type).

一个示例可能有助于明确切入点签名和切入点表达式之间的区别。以下示例定义了一个名为`anyOldTransfer`的切入点,它匹配任何名为`transfer`的方法的执行:

An example may help make this distinction between a pointcut signature and a pointcut expression clear. The following example defines a pointcut named anyOldTransfer that matches the execution of any method named transfer:

  • Java

  • Kotlin

@Pointcut("execution(* transfer(..))") // the pointcut expression
private void anyOldTransfer() {} // the pointcut signature
@Pointcut("execution(* transfer(..))") // the pointcut expression
private fun anyOldTransfer() {} // the pointcut signature

形成 @Pointcut 注解值的切入点表达式是一个常规的 AspectJ 切入点表达式。有关 AspectJ 切入点语言的完整讨论,请参见https://www.eclipse.org/aspectj/doc/released/progguide/index.html[AspectJ 编程指南](并且,有关扩展信息,请参见https://www.eclipse.org/aspectj/doc/released/adk15notebook/index.html[AspectJ 5 开发人员手册])或有关 AspectJ 的书籍之一(例如 Eclipse AspectJ,Colyer 等著,或 AspectJ in Action,Ramnivas Laddad 著)。

The pointcut expression that forms the value of the @Pointcut annotation is a regular AspectJ pointcut expression. For a full discussion of AspectJ’s pointcut language, see the AspectJ Programming Guide (and, for extensions, the AspectJ 5 Developer’s Notebook) or one of the books on AspectJ (such as Eclipse AspectJ, by Colyer et al., or AspectJ in Action, by Ramnivas Laddad).

Supported Pointcut Designators

Spring AOP支持在切入点表达式中使用的以下AspectJ切入点指示符(PCD):

Spring AOP supports the following AspectJ pointcut designators (PCD) for use in pointcut expressions:

  • execution: For matching method execution join points. This is the primary pointcut designator to use when working with Spring AOP.

  • within: Limits matching to join points within certain types (the execution of a method declared within a matching type when using Spring AOP).

  • this: Limits matching to join points (the execution of methods when using Spring AOP) where the bean reference (Spring AOP proxy) is an instance of the given type.

  • target: Limits matching to join points (the execution of methods when using Spring AOP) where the target object (application object being proxied) is an instance of the given type.

  • args: Limits matching to join points (the execution of methods when using Spring AOP) where the arguments are instances of the given types.

  • @target: Limits matching to join points (the execution of methods when using Spring AOP) where the class of the executing object has an annotation of the given type.

  • @args: Limits matching to join points (the execution of methods when using Spring AOP) where the runtime type of the actual arguments passed have annotations of the given types.

  • @within: Limits matching to join points within types that have the given annotation (the execution of methods declared in types with the given annotation when using Spring AOP).

  • @annotation: Limits matching to join points where the subject of the join point (the method being run in Spring AOP) has the given annotation.

Other pointcut types

完整的AspectJ切入点语言支持Spring中不支持的其他切入点指示符:callgetsetpreinitializationstaticinitializationinitializationhandleradviceexecutionwithincodecflowcflowbelowif@this`和@withincode`。在Spring AOP解释的切入点表达式中使用这些切入点指示符将导致抛出`IllegalArgumentException`。

The full AspectJ pointcut language supports additional pointcut designators that are not supported in Spring: call, get, set, preinitialization, staticinitialization, initialization, handler, adviceexecution, withincode, cflow, cflowbelow, if, @this, and @withincode. Use of these pointcut designators in pointcut expressions interpreted by Spring AOP results in an IllegalArgumentException being thrown.

Spring AOP支持的切入点指示符集可能会在未来版本中扩展,以支持更多的AspectJ切入点指示符。

The set of pointcut designators supported by Spring AOP may be extended in future releases to support more of the AspectJ pointcut designators.

由于Spring AOP只将匹配限制为方法执行连接点,因此前面对切入点指示符的讨论给出了比您在AspectJ编程指南中找到的更窄的定义。此外,AspectJ本身具有基于类型的语义,并且在执行连接点处,this`和`target`都指向同一个对象:执行该方法的对象。Spring AOP是一个基于代理的系统,并且区分代理对象本身(绑定到`this)和代理后面的目标对象(绑定到`target`)。

Because Spring AOP limits matching to only method execution join points, the preceding discussion of the pointcut designators gives a narrower definition than you can find in the AspectJ programming guide. In addition, AspectJ itself has type-based semantics and, at an execution join point, both this and target refer to the same object: the object executing the method. Spring AOP is a proxy-based system and differentiates between the proxy object itself (which is bound to this) and the target object behind the proxy (which is bound to target).

由于Spring AOP框架基于代理,因此,目标对象内的调用在定义上不会被拦截。对于JDK代理,只能拦截代理上的公共接口方法调用。使用CGLIB,代理上的公共和受保护的方法调用会被拦截(如果有必要,甚至是包可见的方法)。但是,常见的代理交互应始终通过公共签名设计。

Due to the proxy-based nature of Spring’s AOP framework, calls within the target object are, by definition, not intercepted. For JDK proxies, only public interface method calls on the proxy can be intercepted. With CGLIB, public and protected method calls on the proxy are intercepted (and even package-visible methods, if necessary). However, common interactions through proxies should always be designed through public signatures.

请注意,切入点定义通常与任何被拦截的方法匹配。如果切入点严格上仅为公共的,即使在与CGLIB代理中通过代理进行潜在的非公开交互的情况下,也需要对其进行相应定义。

Note that pointcut definitions are generally matched against any intercepted method. If a pointcut is strictly meant to be public-only, even in a CGLIB proxy scenario with potential non-public interactions through proxies, it needs to be defined accordingly.

如果您的拦截需要包括目标类中的方法调用,甚至其构造函数,请考虑使用 Spring 驱动的 native AspectJ weaving,而不是基于 Spring 代理的 AOP 框架。这构成了一种具有不同特征的不同 AOP 使用模式,因此务必在做出决定之前让自己熟悉编织。

If your interception needs include method calls or even constructors within the target class, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving before making a decision.

Spring AOP还支持一个名为`bean`的附加PCD。此PCD允许您将连接点的匹配限制为特定的命名Spring Bean或一组命名的Spring Bean(使用通配符时)。bean PCD具有以下形式:

Spring AOP also supports an additional PCD named bean. This PCD lets you limit the matching of join points to a particular named Spring bean or to a set of named Spring beans (when using wildcards). The bean PCD has the following form:

bean(idOrNameOfBean)

idOrNameOfBean 标记可以是任何 Spring bean 的名称。提供了使用 * 字符的有限通配符支持,因此,如果你为你的 Spring bean 建立一些命名约定,你可以编写一个 bean PCD 表达式来选择它们。与其他切入点设计符一样,bean PCD 也可以与 &&(与)、||(或)和 !(否定)运算符一起使用。

The idOrNameOfBean token can be the name of any Spring bean. Limited wildcard support that uses the * character is provided, so, if you establish some naming conventions for your Spring beans, you can write a bean PCD expression to select them. As is the case with other pointcut designators, the bean PCD can be used with the && (and), || (or), and ! (negation) operators, too.

bean PCD 仅在 Spring AOP 中得到支持,而不是在 native AspectJ 编织中。这是对 AspectJ 定义的标准 PCD 的特定于 Spring 的扩展,因此无法用于在 @Aspect 模型中声明的方面。

The bean PCD is supported only in Spring AOP and not in native AspectJ weaving. It is a Spring-specific extension to the standard PCDs that AspectJ defines and is, therefore, not available for aspects declared in the @Aspect model.

bean PCD 在实例级别(建立在 Spring bean 名称概念之上)而不是仅在类型级别(编织基础的 AOP 受此限制)上操作。基于实例的切入点设计符是 Spring 的基于代理的 AOP 框架及其与 Spring bean 工厂紧密集成的特殊功能,在该框架中,按名称识别特定 bean 是自然而直接的。

The bean PCD operates at the instance level (building on the Spring bean name concept) rather than at the type level only (to which weaving-based AOP is limited). Instance-based pointcut designators are a special capability of Spring’s proxy-based AOP framework and its close integration with the Spring bean factory, where it is natural and straightforward to identify specific beans by name.

Combining Pointcut Expressions

你可以使用 &&, ||! 组合切入点表达式。你还可以按名称引用切入点表达式。以下示例显示了三个切入点表达式:

You can combine pointcut expressions by using &&, || and !. You can also refer to pointcut expressions by name. The following example shows three pointcut expressions:

Java
public class Pointcuts {

	@Pointcut("execution(public * *(..))")
	public void publicMethod() {} (1)

	@Pointcut("within(com.xyz.trading..*)")
	public void inTrading() {} (2)

	@Pointcut("publicMethod() && inTrading()")
	public void tradingOperation() {} (3)
}
1 publicMethod matches if a method execution join point represents the execution of any public method.
2 inTrading matches if a method execution is in the trading module.
3 tradingOperation matches if a method execution represents any public method in the trading module.
Kotlin
class Pointcuts {

	@Pointcut("execution(public * *(..))")
	fun publicMethod() {} (1)

	@Pointcut("within(com.xyz.trading..*)")
	fun inTrading() {} (2)

	@Pointcut("publicMethod() && inTrading()")
	fun tradingOperation() {} (3)
}
4 publicMethod matches if a method execution join point represents the execution of any public method.
5 inTrading matches if a method execution is in the trading module.
6 tradingOperation matches if a method execution represents any public method in the trading module.

最好像上面所示那样,用较小的 命名切入点 构建更复杂的切入点表达式。在按名称引用切入点时,会应用正常的 Java 可见性规则(你可以看到同类型中的 private 切入点、层次结构中的 protected 切入点、任何地方的 public 切入点,依此类推)。可见性不会影响切入点匹配。

It is a best practice to build more complex pointcut expressions out of smaller named pointcuts, as shown above. When referring to pointcuts by name, normal Java visibility rules apply (you can see private pointcuts in the same type, protected pointcuts in the hierarchy, public pointcuts anywhere, and so on). Visibility does not affect pointcut matching.

Sharing Named Pointcut Definitions

在使用企业应用程序时,开发人员经常需要从多个方面引用应用程序的模块和特定操作集。我们建议为此目的定义一个专用的类,以捕获常用的 命名字入点 表达式。此类通常类似于以下 CommonPointcuts 示例(尽管你如何命名该类由你决定):

When working with enterprise applications, developers often have the need to refer to modules of the application and particular sets of operations from within several aspects. We recommend defining a dedicated class that captures commonly used named pointcut expressions for this purpose. Such a class typically resembles the following CommonPointcuts example (though what you name the class is up to you):

  • Java

  • Kotlin

import org.aspectj.lang.annotation.Pointcut;

public class CommonPointcuts {

	/**
	 * A join point is in the web layer if the method is defined
	 * in a type in the com.xyz.web package or any sub-package
	 * under that.
	 */
	@Pointcut("within(com.xyz.web..*)")
	public void inWebLayer() {}

	/**
	 * A join point is in the service layer if the method is defined
	 * in a type in the com.xyz.service package or any sub-package
	 * under that.
	 */
	@Pointcut("within(com.xyz.service..*)")
	public void inServiceLayer() {}

	/**
	 * A join point is in the data access layer if the method is defined
	 * in a type in the com.xyz.dao package or any sub-package
	 * under that.
	 */
	@Pointcut("within(com.xyz.dao..*)")
	public void inDataAccessLayer() {}

	/**
	 * A business service is the execution of any method defined on a service
	 * interface. This definition assumes that interfaces are placed in the
	 * "service" package, and that implementation types are in sub-packages.
	 *
	 * If you group service interfaces by functional area (for example,
	 * in packages com.xyz.abc.service and com.xyz.def.service) then
	 * the pointcut expression "execution(* com.xyz..service.*.*(..))"
	 * could be used instead.
	 *
	 * Alternatively, you can write the expression using the 'bean'
	 * PCD, like so "bean(*Service)". (This assumes that you have
	 * named your Spring service beans in a consistent fashion.)
	 */
	@Pointcut("execution(* com.xyz..service.*.*(..))")
	public void businessService() {}

	/**
	 * A data access operation is the execution of any method defined on a
	 * DAO interface. This definition assumes that interfaces are placed in the
	 * "dao" package, and that implementation types are in sub-packages.
	 */
	@Pointcut("execution(* com.xyz.dao.*.*(..))")
	public void dataAccessOperation() {}

}
import org.aspectj.lang.annotation.Pointcut

class CommonPointcuts {

	/**
	 * A join point is in the web layer if the method is defined
	 * in a type in the com.xyz.web package or any sub-package
	 * under that.
	 */
	@Pointcut("within(com.xyz.web..*)")
	fun inWebLayer() {}

	/**
	 * A join point is in the service layer if the method is defined
	 * in a type in the com.xyz.service package or any sub-package
	 * under that.
	 */
	@Pointcut("within(com.xyz.service..*)")
	fun inServiceLayer() {}

	/**
	 * A join point is in the data access layer if the method is defined
	 * in a type in the com.xyz.dao package or any sub-package
	 * under that.
	 */
	@Pointcut("within(com.xyz.dao..*)")
	fun inDataAccessLayer() {}

	/**
	 * A business service is the execution of any method defined on a service
	 * interface. This definition assumes that interfaces are placed in the
	 * "service" package, and that implementation types are in sub-packages.
	 *
	 * If you group service interfaces by functional area (for example,
	 * in packages com.xyz.abc.service and com.xyz.def.service) then
	 * the pointcut expression "execution(* com.xyz..service.*.*(..))"
	 * could be used instead.
	 *
	 * Alternatively, you can write the expression using the 'bean'
	 * PCD, like so "bean(*Service)". (This assumes that you have
	 * named your Spring service beans in a consistent fashion.)
	 */
	@Pointcut("execution(* com.xyz..service.*.*(..))")
	fun businessService() {}

	/**
	 * A data access operation is the execution of any method defined on a
	 * DAO interface. This definition assumes that interfaces are placed in the
	 * "dao" package, and that implementation types are in sub-packages.
	 */
	@Pointcut("execution(* com.xyz.dao.*.*(..))")
	fun dataAccessOperation() {}

}

你可以在任何需要切入点表达式的任何地方引用此类中定义的切入点,方法是引用类与 @Pointcut 方法的名称结合的全限定名称。例如,为了使服务层具有事务性,你可以编写以下代码引用`com.xyz.CommonPointcuts.businessService()` 命名字入点

You can refer to the pointcuts defined in such a class anywhere you need a pointcut expression by referencing the fully-qualified name of the class combined with the @Pointcut method’s name. For example, to make the service layer transactional, you could write the following which references the com.xyz.CommonPointcuts.businessService() named pointcut:

<aop:config>
	<aop:advisor
		pointcut="com.xyz.CommonPointcuts.businessService()"
		advice-ref="tx-advice"/>
</aop:config>

<tx:advice id="tx-advice">
	<tx:attributes>
		<tx:method name="*" propagation="REQUIRED"/>
	</tx:attributes>
</tx:advice>

<aop:config><aop:advisor> 元素在 Schema-based AOP Support 中进行讨论。事务元素在 Transaction Management 中进行讨论。

The <aop:config> and <aop:advisor> elements are discussed in Schema-based AOP Support. The transaction elements are discussed in Transaction Management.

Examples

Spring AOP 用户最有可能最常使用 execution 切入点设计符。执行表达式的格式如下:

Spring AOP users are likely to use the execution pointcut designator the most often. The format of an execution expression follows:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)

除了返回类型模式(前面代码段中的 ret-type-pattern)、名称模式和参数模式之外,所有部分都是可选的。返回类型模式确定连接点必须具有的方法返回类型才能匹配。 is most frequently used as the returning type pattern. It matches any return type. A fully-qualified type name matches only when the method returns the given type. The name pattern matches the method name. You can use the 通配符作为名称模式的一部分或全部。如果你指定了一个声明类型模式,请包含一个尾随 . 将其连接到名称模式分量。参数模式略微复杂一些:() 匹配不包含任何参数的方法,而 (..) 匹配任意数量(零个或更多)的参数。() pattern matches a method that takes one parameter of any type. (,String) 匹配接受两个参数的方法。第一个参数可以是任何类型,而第二个参数必须是 String。有关详细信息,请参阅 AspectJ 编程指南的https://www.eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html[语言语义]部分。

All parts except the returning type pattern (ret-type-pattern in the preceding snippet), the name pattern, and the parameters pattern are optional. The returning type pattern determines what the return type of the method must be in order for a join point to be matched. is most frequently used as the returning type pattern. It matches any return type. A fully-qualified type name matches only when the method returns the given type. The name pattern matches the method name. You can use the wildcard as all or part of a name pattern. If you specify a declaring type pattern, include a trailing . to join it to the name pattern component. The parameters pattern is slightly more complex: () matches a method that takes no parameters, whereas (..) matches any number (zero or more) of parameters. The () pattern matches a method that takes one parameter of any type. (,String) matches a method that takes two parameters. The first can be of any type, while the second must be a String. Consult the Language Semantics section of the AspectJ Programming Guide for more information.

以下示例显示了一些常见的切入点表达式:

The following examples show some common pointcut expressions:

  • The execution of any public method:[indent="0",subs="verbatim"] execution(public * *(..))

  • The execution of any method with a name that begins with set:[indent="0",subs="verbatim"] execution(* set*(..))

  • The execution of any method defined by the AccountService interface:[indent="0",subs="verbatim"] execution(* com.xyz.service.AccountService.*(..))

  • The execution of any method defined in the service package:[indent="0",subs="verbatim"] execution(* com.xyz.service..(..))

  • The execution of any method defined in the service package or one of its sub-packages:[indent="0",subs="verbatim"] execution(* com.xyz.service...(..))

  • Any join point (method execution only in Spring AOP) within the service package:[indent="0",subs="verbatim"] within(com.xyz.service.*)

  • Any join point (method execution only in Spring AOP) within the service package or one of its sub-packages:[indent="0",subs="verbatim"] within(com.xyz.service..*)

  • Any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface:[indent="0",subs="verbatim"] this(com.xyz.service.AccountService)

this 更常用于绑定形式。请参阅 Declaring Advice 的章节,以了解如何在建议正文中使用代理对象。

this is more commonly used in a binding form. See the section on Declaring Advice for how to make the proxy object available in the advice body.

  • Any join point (method execution only in Spring AOP) where the target object implements the AccountService interface:[indent="0",subs="verbatim"] target(com.xyz.service.AccountService)

target 更常用于绑定形式。请参阅 Declaring Advice 章节,以了解如何在建议正文中使用目标对象。

target is more commonly used in a binding form. See the Declaring Advice section for how to make the target object available in the advice body.

  • Any join point (method execution only in Spring AOP) that takes a single parameter and where the argument passed at runtime is Serializable:[indent="0",subs="verbatim"] args(java.io.Serializable)

args 更常用于绑定形式。请参阅 Declaring Advice 章节,以了解如何在建议正文中使用方法参数。

args is more commonly used in a binding form. See the Declaring Advice section for how to make the method arguments available in the advice body.

请注意,本示例中给出的切入点与 execution(**(java.io.Serializable)) 不同。如果在运行时传递的参数是 Serializable,则 args 版本匹配,如果方法签名声明了一个类型为 Serializable 的单个参数,则 execution 版本匹配。

Note that the pointcut given in this example is different from execution(* *(java.io.Serializable)). The args version matches if the argument passed at runtime is Serializable, and the execution version matches if the method signature declares a single parameter of type Serializable. * Any join point (method execution only in Spring AOP) where the target object has a @Transactional annotation:[indent="0",subs="verbatim"] @target(org.springframework.transaction.annotation.Transactional)

您还可以在绑定形式中使用 @target。请参阅 Declaring Advice 章节,以了解如何在建议正文中使用注释对象。

You can also use @target in a binding form. See the Declaring Advice section for how to make the annotation object available in the advice body.

  • Any join point (method execution only in Spring AOP) where the declared type of the target object has an @Transactional annotation:[indent="0",subs="verbatim"] @within(org.springframework.transaction.annotation.Transactional)

您还可以在绑定形式中使用 @within。请参阅 Declaring Advice 章节,以了解如何在建议正文中使用注释对象。

You can also use @within in a binding form. See the Declaring Advice section for how to make the annotation object available in the advice body.

  • Any join point (method execution only in Spring AOP) where the executing method has an @Transactional annotation:[indent="0",subs="verbatim"] @annotation(org.springframework.transaction.annotation.Transactional)

您还可以在绑定形式中使用 @annotation。请参阅 Declaring Advice 章节,以了解如何在建议正文中使用注释对象。

You can also use @annotation in a binding form. See the Declaring Advice section for how to make the annotation object available in the advice body.

  • Any join point (method execution only in Spring AOP) which takes a single parameter, and where the runtime type of the argument passed has the @Classified annotation:[indent="0",subs="verbatim"] @args(com.xyz.security.Classified)

您还可以在绑定形式中使用 @args。请参阅 Declaring Advice 章节,以了解如何在建议正文中使用注释对象。

You can also use @args in a binding form. See the Declaring Advice section how to make the annotation object(s) available in the advice body.

  • Any join point (method execution only in Spring AOP) on a Spring bean named tradeService:[indent="0",subs="verbatim"] bean(tradeService)

  • Any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression *Service:[indent="0",subs="verbatim"] bean(*Service)

Writing Good Pointcuts

在编译期间,AspectJ 处理切入点以优化匹配性能。检查代码并确定每个连接点是否(静态地或动态地)匹配给定切入点是一个昂贵的过程。(动态匹配意味着无法从静态分析中完全确定匹配,并且在代码运行时将测试放入代码中以确定是否有实际匹配)。首次遇到切入点声明时,AspectJ 会将其重写为匹配过程的最佳形式。这意味着什么?基本上,切入点被重写为 DNF(析取范式),并且切入点的组件被排序,以便首先检查成本较低的组件。这意味着你不必担心了解各种切入点设计符的性能,并且可以在切入点声明中以任何顺序提供它们。

During compilation, AspectJ processes pointcuts in order to optimize matching performance. Examining code and determining if each join point matches (statically or dynamically) a given pointcut is a costly process. (A dynamic match means the match cannot be fully determined from static analysis and that a test is placed in the code to determine if there is an actual match when the code is running). On first encountering a pointcut declaration, AspectJ rewrites it into an optimal form for the matching process. What does this mean? Basically, pointcuts are rewritten in DNF (Disjunctive Normal Form) and the components of the pointcut are sorted such that those components that are cheaper to evaluate are checked first. This means you do not have to worry about understanding the performance of various pointcut designators and may supply them in any order in a pointcut declaration.

但是,AspectJ 只能够处理它被告知的内容。为了获得最佳匹配性能,你应该考虑要实现的目标,并在定义中尽可能缩小匹配的搜索空间。现有的设计符自然分为三种类型之一:种类、范围和上下文:

However, AspectJ can work only with what it is told. For optimal performance of matching, you should think about what you are trying to achieve and narrow the search space for matches as much as possible in the definition. The existing designators naturally fall into one of three groups: kinded, scoping, and contextual:

  • Kinded designators select a particular kind of join point: execution, get, set, call, and handler.

  • Scoping designators select a group of join points of interest (probably of many kinds): within and withincode

  • Contextual designators match (and optionally bind) based on context: this, target, and @annotation

一个写得好的切入点应至少包括前两种类型(种类和范围)。你可以包括上下文设计符来根据连接点上下文进行匹配或绑定该上下文以在建议中使用。仅提供一个种类设计符或仅提供一个上下文设计符可以工作,但由于需要额外的处理和分析,可能会影响编织性能(时间和内存的使用)。范围设计符的匹配速度非常快,使用它们意味着 AspectJ 可以非常快速地忽略不应该进一步处理的连接点组。如果可能,一个好的切入点应该始终包括一个。

A well written pointcut should include at least the first two types (kinded and scoping). You can include the contextual designators to match based on join point context or bind that context for use in the advice. Supplying only a kinded designator or only a contextual designator works but could affect weaving performance (time and memory used), due to extra processing and analysis. Scoping designators are very fast to match, and using them means AspectJ can very quickly dismiss groups of join points that should not be further processed. A good pointcut should always include one if possible.