Select connection points by tangent point

The most important point about Spring AOP's AspectJ pointcut is that Spring only supports a subset of the AspectJ pointcut designator. Let's recall that Spring is proxy-based, and some pointcut expressions are incompatible with proxy-based AOP.

off. Table 4.1 lists the AspectJ pointcut indicators supported by Spring AOP.

 

 

When we look at the indicators supported by Spring as shown above, notice that only the execution indicator is the one that actually executes the match, while the other indicators are used to limit the match. This shows that the execution indicator is the one we mostly use when writing pointcut definitions. On this basis, we use other indicators to limit the matched pointcuts.

 

1.1 Write a pointcut

In order to describe aspects in Spring, we need a topic to define the pointcuts of the aspect. For this, we define a Performance interface:

Performance can represent any type of live performance, such as a stage play, movie or concert. Suppose we want to write notifications triggered by Performance's perform() method. Figure 4.4 shows a pointcut expression that can be set when the perform() method

The call to trigger the notification when the method executes.

 

We use the execution() indicator to select the perform() method of Performance. A method expression begins with a "*" sign, indicating that we don't care about the type of the method's return value. Then, we specify the fully qualified class name and method name. For the method parameter list, we use two dots (..) to indicate that the pointcut selects any perform() method, regardless of the method's input parameters.

 

Now suppose the pointcut we need to configure matches only the concert package. In this scenario, the within() indicator can be used to restrict matching, as shown in Figure 4.5.

 

 

Note that we used the "&&" operator to connect the execution() and within() indicators together to form an AND relation (the pointcut must match all indicators). Similarly, we can use the "||" operator to identify an OR relationship, while the "!" operator

operator to identify a not operation.

Because "&" has a special meaning in XML, we can use and instead of "&&" when describing pointcuts in Spring's XML configuration. Likewise, or and not can be used in place of "||" and "!", respectively.

 

1.2 Select bean in pointcut

In addition to the indicators listed in Table 4.1, Spring also introduces a new bean() indicator, which allows us to use the bean's ID to identify a bean in pointcut expressions. bean() takes a bean ID or a bean name as an argument to restrict pointcuts to match only specific beans.

 

Here, we want to apply the notification when the Performance's perform() method is executed, but limit the bean's ID to woodstock.

In some cases, it might make sense to limit the pointcut to a specific bean, but we can also use the non-operation to apply advice for other beans than a specific ID:

 

 

Guess you like

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