AspectJ expression to be used when configuring SPRINGAOP

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).

AspectJ expression to be used when configuring SPRINGAOP

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.