Spring aspectj Execution Expressions-Memo Notes

Source: Internet
Author: User

ASPECTJ pointcut Syntax definition

When using the spring framework to configure AOP, you need to define Pointcut "pointcuts" either through XML configuration files or annotations.

For example, define pointcut expression Execution (* Com.sample.service.impl: *.*(..))

Execution () is the most commonly used pointcut function, and its syntax is as follows:

The entire expression can be divided into five parts:

1. Execution (): The body of an expression.

2, the first * Number: Indicates the return type, the * number denotes all types.

3. Package Name: Indicates the package name that needs to be intercepted, and the following two periods represent all the child packages of the current package and the current package, Com.sample.service.impl the package, and the descendants of all classes.

4, the second * Number: denotes the class name, the * number denotes all classes.

5, * (..): The last asterisk denotes the method name, the * number denotes all methods, the following parentheses indicate the parameters of the method, and two periods represent any parameters.

Execution expression of ASPECTJ

Execution ()

Execution () is the most commonly used pointcut function, and its syntax is as follows:

Execution (< modifier mode;? < return type mode > < method name mode > (< parameter mode >) < exception mode;?) In addition to return type mode, method name mode, and parameter mode, other items are optional. Rather than directly explaining the rules of use of the method, it is better to understand them through specific examples. Below, we give a variety of examples using the execution () function.

1) Defining pointcuts through method signatures

Execution (Public * * (..)) L

Matches the public method of all target classes, but does not match the Smartseller and protected void Showgoods () methods. The first * represents the return type, the second one represents the method name, and the. A method that represents an arbitrary entry parameter;

Execution (* *to (..)) L

Matches all methods of the target class with the to suffix. It matches the Greetto () and Serveto () methods of Naivewaiter and Naughtywaiter. The first * represents the return type, while the *to represents any method with the to suffix;

2) defining pointcuts through classes

Execution (* com.baobaotao.waiter.* (..)) L

Matches all methods of the waiter interface, which match the Greetto () and Serveto () methods of the Naivewaiter and Naughtywaiter classes. The first * represents the return of any type, com.baobaotao.waiter.* represents all methods in the waiter interface;

Execution (* com.baobaotao.waiter+.* (..)) L

A method that matches the waiter interface and all of its implementation classes, matching not only the Greetto () and Serveto () of the Naivewaiter and Naughtywaiter classes, but also the methods defined by the two waiter interfaces, as well as the naivewaiter# Smile () and Naughtywaiter#joke () are two methods that are not defined in the waiter interface.

3) defining pointcuts through class packages

In the class name pattern string, ". *" represents all classes under the package, while the ". * "denotes all classes under Package, descendant package.

Execution (* com.baobaotao.* (..)) L

Match all methods of all classes under the Com.baobaotao package;

Execution (* Com.baobaotao). *(..)) L

All methods that match the Com.baobaotao package and all classes under the descendants package, such as Com.baobaotao.dao,com.baobaotao.servier and all the methods of all classes under the Com.baobaotao.dao.user package, match. “..” When it appears in the class name, it must be followed by "*", which represents all classes under the package, descendant packages;

Execution (* com.. *.*dao.find* (..)) L

Any package that matches the package name prefix to COM under the class name suffix is a DAO method, and the method name must be prefixed with find. Methods such as Com.baobaotao.userdao#findbyuserid () and Com.baobaotao.dao.forumdao#findbyid () all match pointcuts.

4) Defining a pointcut by means of a parameter entry

The method entry part of the pointcut expression is more complex and can be used with "*" and ".." Wildcards, where "*" represents any type of argument, while ".." Represents any type parameter and has an unlimited number of parameters.

Execution (* joke (String,int))) L

Matches the joke (String,int) method, and the first entry parameter of the joke () method is a String, and the second entry parameter is int. It matches the Naughtywaiter#joke (String,int) method. If the entry type in the method is a class under the Java.lang package, you can use the class name directly, or you must use the fully qualified class name, such as joke (Java.util.list,int);

Execution (* joke (string,*))) L

Matches the joke () method in the target class, the first entry parameter is a String, the second entry parameter can be any type, such as joke (string s1,string s2) and joke (string s1,double D2), but joke (string S1,double d2,string S3) does not match;

Execution (* joke (String,..))) L

Matches the joke () method in the target class, the first parameter of the method is a string, which can have any of the entry parameters, such as joke (string S1), joke (string s1,string s2), and joke (string s1,double D2,string S3) are matched.

Execution (* joke (object+))) L

Match the Joke () method in the target class, the method has an entry parameter, and the entry parameter is either the object type or the subclass of the class. It matches joke (String S1) and joke (Client C). If the tangent we define is execution (* joke (object)), only joke (object object) is matched and does not match joke (String cc) or joke (Client c).

Args () and @args ()

The argument to the args () function is the class name, and the entry of the @args () function must be the class name of the annotation class. Although Args () allows the use of the + wildcard suffix after the class name, the wildcard does not make sense here: the same is true for adding and not adding effects.

1) args ()

The function accepts a class name that indicates that the target class method is matched to the specified class by type, as in the following example:

Args (Com.baobaotao.Waiter)

Indicates that the run-time entry parameter is a method of type waiter, which differs from execution (* * (Com.baobaotao.Waiter)) in that the latter is for the signature of a class method, whereas the former is for the runtime's type of entry. If Args (com.baobaotao.Waiter) matches both addwaiter (waiter waiter) and Addnaivewaiter (Naivewaiter naivewaiter), Execution (* * (Com.baobaotao.Waiter)) matches only Addwaiter (waiter waiter) method; in fact, args (Com.baobaotao.Waiter) is equivalent to execution (* * ( com.baobaotao.waiter+), of course, is also equivalent to Args (com.baobaotao.waiter+).

2) @args ()

The function takes the class name of an annotation class, and the method matches the Pointcut when the method's run-time import parameter annotation sends the specified annotation. The matching rules for this pointcut function are not easy to understand, so we'll explain this in more detail below:

Figure 4 @arg (M) match (1)

T0, T1, T2, T3 have an inheritance relationship, assuming that the signature of the target class method is fun (T1 t), its entry is T1, and the tangent of the tangent is defined as @args (M), and the T2 class is labeled @m. When the fun (T1 T) incoming object is T2 or T3, the method matches the tangent point defined by the @args (M) declaration;

Looking at the following scenario, suppose the method signature is fun (T1 t), the input parameter is for T1, and the class labeled @m is T0, and when Funt (T1 T) is passed into T1, T2, T3 instances, the Pointcut @args (M) is not matched.

Figure 5 @arg (M) match (2)

In the inheritance tree of a class, the ① point is the position of the parameter type in the class inheritance tree in the method signature, which we call the entry type point, and ② is the position of the class that annotated the @m annotation in the class inheritance tree, which we call the annotation point. Judging whether the method matches the @agrs (M) tangent point at run time, it can be judged based on the relative position of the ① and ② points in the class inheritance tree:

1) If the annotation point ② in the class inheritance tree is higher than the entry type point ①, then the target method cannot match the tangent @args (M), 5;

2) If the annotation point ② in the class inheritance tree is lower than the entry parameter type point ①, the method matches the @args (M) Pointcut, as shown in 4, when the annotation point is in the same class and its descendant class as the method.

For a specific example, let's say we define a pointcut like this: @args (com.baobaotao.Monitorable), and if Naivewaiter labels @monitorable, for waitermanager# Addwaiter (Waiter W) method, if the entry parameter is a naivewaiter or its subclass object, the method matches the pointcut, and if the entry is a Naughtywaiter object, the pointcut is not matched. Waitermanager#addnaivewaiter (naivewaiter W) does not match the pointcut if waiter labels @monitorable but Naivewaiter does not label @monitorable. This is because the annotation point (at waiter) is higher than the entry type point (Naivewaiter).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.