Original springAop aspect execution translation and analysis

Official link: https://docs.spring.io/spring/docs/4.3.16.RELEASE/spring-framework-reference/htmlsingle/#aop-pointcuts-examples   The specific location is at Examples of 11.2.3 Declaring a pointcut

For reference, the translation is as follows:

Users often use the execution expression in Spring AOP. The format of the expression is as follows:

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

In the above expressions, ret-type-pattern, name-pattern, and param-pattern are required fields, and others are optional. The ret-type-pattern is configured for the method return value, and the aspect is the method whose return value matches. Often we configure the ret-type-pattern as *, which means matching any return value type.

name-pattern matches the method name, configuration * also means matching all methods, declaring-type-pattern connects the name-pattern method name through `.`,

The param-pattern method parameter expression is a little more complicated: () means match a method without parameters, (..) means match any method, (*) means match a method with one parameter, the parameter can be any type, (*, String) represents a method that matches with two parameters, the first can be of any type, and the second must be of type String.

The following is a summary of several examples of official executions:

  • the execution of any public method:    the aspect is all public methods
execution( public * *(..))       //public is the scope of the modifiers-pattern method * is ret-type-pattern * is name-pattern (..) is param-pattern
  • the execution of any method with a name beginning with "set":
execution(* set*(..))   //* is ret-type-pattern set* is name-pattern (..) is param-pattern
  • the execution of any method defined by the AccountService interface:
  • In order to quickly read the configuration information of the execution, you can separate the three required items of the configuration information inside, and then read the meaning of the other contents.
execution(* com.xyz.service.AccountService.*(..))

First, cut out the three required items according to the above picture. According to the translated content, you should be able to know the meaning quickly, so only the analysis is left:

What is the meaning of com.xyz.service.AccountService., of course, according to the above translation, the aspect is defined in the interface class AccountService,

Corresponds to the scope configuration of the declaring-type-pattern aspect in the expression.

When all the information is combined, it can be intuitively understood as: the aspect is that all methods in com.xyz.service.AccountService do not restrict any parameters and do not restrict any return value. The concise point is all methods in com.xyz.service.AccountService.

  • the execution of any method defined in the service package:
execution(* com.xyz.service.*.*(..))

Analysis and cutting the remaining com.xyz.service.*. The scope is in the com.xyz.service package, according to the English description.

  • the execution of any method defined in the service package or a sub-package:
execution(* com.xyz.service..*.*(..))

The remaining com.xyz.service..*.* scope of analysis and cutting is in the com.xyz.service package and its sub-packages, according to the English description.

Guess you like

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