Spring ASPECTJ pointcut Syntax detailed

Source: Internet
Author: User
Tags anonymous aop deprecated
1. ASPECTJ pointcut indicator supported by Spring AOP


Pointcut indicators are used to indicate pointcut expression purposes, where there is currently only one connection point for execution methods in spring AOP, the ASPECTJ pointcut indicator supported by spring AOP is as follows:

execution: The connection point used to match the execution of the method;

within: used to match method execution within a specified type;

This : used to match the execution method of the current AOP proxy object type, note that the type of the AOP proxy object matches, which may include the introduction of interface and type matching;

Target : The execution method used to match the current target object type; Note that the target object type matches, so that the introduction interface is not included and the type match;

args: the method used to match the current execution of the passed parameter to the specified type;

@within: used to match so hold the method within the specified annotation type;

@target: The execution method used to match the current target object type, where the target object holds the specified annotation;

@args: used to match the current execution of the method passed in the parameter holding the specified annotation execution;

@annotation: The method used to match the current execution method holding the specified annotation;

Bean: Spring AOP Extended, ASPECTJ does not have an indicator for the execution of a Bean object that matches a particular name;

reference pointcut: indicates references to other named Pointcuts, only @apectj style support, schema style is not supported.

The pointcut indicators supported by the ASPECTJ Pointcut are also: Call, get, set, Preinitialization, Staticinitialization, initialization, Handler, Adviceexecution, Withincode, Cflow, Cflowbelow, if, @this, @withincode; but spring AOP does not currently support these indicators, Using these indicators throws a IllegalArgumentException exception. These indicators spring AOP may be extended at a later time.

2. Naming and anonymous entry points

Named Pointcuts can be referenced by other pointcuts, and anonymous pointcuts are not allowed. Only @aspectj supports named Pointcuts, and schema styles do not support named Pointcuts.

As shown below, @AspectJ reference named Pointcuts using the following methods:

3. Type matching syntax

First let's look at the following wildcard characters for ASPECTJ type matching:

*: matches any number of characters;

.. : matches any number of characters, such as matching any number of sub-packages in type mode, and any number of parameters in the method parameter pattern.

+: matches a subtype of the specified type; only the suffix can be placed behind the type pattern.

  For example:

java.lang.String     match String type;  
java.*. String       matches the string type under any "one-level sub-package" Under the Java package  , such as matching java.lang.String, but does not match java.lang.ss.String  
Java: *             matches any type of Java package and any sub-packages, such as matching java.lang.String, java.lang.annotation.Annotation  
java.lang.*ing      Matches the type ending with ing in any Java.lang package;  
java.lang.number+   Matches the self-type of any number under the Java.lang package, such as matching java.lang.Integer, also matching Java.math.BigInteger  

Then look at the specific type of match expression:

Annotations. Modifier? Returns a value type type declaration? Method name (parameter list) exception list.  

annotations: Optional, Method-held annotations, such as @deprecated;

modifiers: optional, such as public, protected;

return value type: required, can be any type mode; "*" denotes all types;

type declaration: optional, can be any type of mode;

method Name: required, you can use "*" for pattern matching;

parameter list:"()" means that the method does not have any parameters; "(..)" Indicates that a method that matches an arbitrary parameter is accepted, "(..., java.lang.String)" means that a match to the parameter that accepts the java.lang.String type ends, and that a method with any arguments can be accepted in front of it; "(Java.lang.String,..)" Represents a method that matches a parameter that accepts a java.lang.String type, and can accept any argument behind it; "(*,java.lang.string)" Represents a method that matches an argument that accepts a java.lang.String type and accepts an arbitrary type parameter in front of it;

exception list: optional, with "throws exception fully qualified Name list" declaration, the exception fully qualified name list if there are multiple "," split, such as throws Java.lang.IllegalArgumentException, Java.lang.ArrayIndexOutOfBoundsException.

4. Combination Pointcut expression

AspectJ use and (&&), or (| | ), non-(. ) to combine pointcut expressions.

In the schema style, the escape character "&&" is required to use "&&" in XML To replace it, so it is inconvenient, so spring ASP provides and, or, not to replace &&, | | 、。。

   

Pointcut Use Example

A, execution: Use the "Execution (method expression)" matching method to execute;

Mode

Description

Public * * (..)

Execution of any public method

* Cn.javass. Ipointcutservice.* ()

Any Cn.javass method in the Ipointcutservice interface under the packet and all sub-packages

* Cn.javass. *.*(..)

Cn.javass any method of any class under the package and all sub-packages

* Cn.javass. ipointcutservice.* (*)

Cn.javass Package and all sub-packages under Ipointcutservice interface any only one parameter method

* (!cn.javass. ipointcutservice+). * (..)

Any method that is not a "Cn.javass packet and all sub-packages Ipointcutservice interfaces and sub-types"

* Cn.javass. Ipointcutservice+.* ()

Any Cn.javass method of Ipointcutservice interface and sub-type under the packet and all sub-packages

* Cn.javass. ipointcut*.test* (Java.util.Date)

Cn.javass Package and all sub-packages the Ipointcut prefix type is a method that starts with test with only one parameter type of java.util.Date, noting that the match is matched based on the parameter type of the method signature, not as defined by the type of argument passed in at execution time: public void Test (Object obj), which does not match even if it is passed into java.util.Date;

* Cn.javass.  ipointcut*.test* (..) Throws IllegalArgumentException, ArrayIndexOutOfBoundsException

Cn.javass any method of Ipointcut prefix type under package and all sub-packages, and throws IllegalArgumentException and ArrayIndexOutOfBoundsException exceptions

* (Cn.javass. ipointcutservice+&& java.io.serializable+). * (..)

Any method that implements the type of Ipointcutservice interface and java.io.Serializable interface under the Cn.javass package and all sub-packages

@java. lang.deprecated * * (..)

Any method that holds @java.lang.deprecated annotations

@java. lang.deprecated @cn. Javass. Secure * * (..)

Any holding @java.lang.deprecated and @cn.javass. Methods for secure annotations

@ (java.lang.Deprecated | | cn.javass. Secure) * * (..)

Any hold @java.lang.deprecated or @ cn.javass. Methods for secure annotations

(@cn. Javass. Secure *) * (..)

Any return value type holds @cn.javass. Secure method

* (@cn. Javass. Secure *). * (..)

Any type that defines the method holds @cn.javass. Secure method

* * (@cn. Javass. Secure (*), @cn. Javass. Secure (*))

Any signature method with two parameters, and the two parameters are marked with @ Secure, such as public void Test (@Secure string str1, @Secure string str1);

* * (@ cn.javass. Secure *) or * * (@ cn.javass). Secure *)

Any method with one parameter, and the parameter type holds @ Cn.javass. Secure, such as public void test (model model), and @secure annotations on the model class

* * (@cn. Javass. Secure (@cn. Javass. Secure *), @ cn.javass. Secure (@cn. Javass. Secure *))

Any method with two arguments, and both parameters are @ Cn.javass. Secure is marked, and the type of both parameters holds @ Cn.javass. Secure;

* * (Java.util.map<cn.javass. Model, Cn.javass. Model>,..)

Any method with a Java.util.Map parameter, and the parameter type is < cn.javass: Model, Cn.javass. Model > is a generic parameter; Note that only the first parameter is Java.util.Map, excluding subtypes, such as public void Test (Hashmap<model, model> Map, String str); You must use "* * * (java.util.hashmap<cn.javass. Model,cn.javass. Model>,..) " The public void test (map map, int i) will not match, because the generic parameter does not match

* * (java.util.collection< @cn. Javass. Secure *>)

Any method with one parameter (type java.util.Collection), and the parameter type is a generic parameter that holds @cn.javass on the generic parameter type: Secure annotations, such as public void Test (collection<model> Collection); The model type holds @cn.javass. Secure

* * (JAVA.UTIL.SET<? extends hashmap>)

Any method with one parameter, and the passed parameter type is a generic parameter, the generic parameter type inherits from the HashMap;Spring AOP test does not work properly now

* * (JAVA.UTIL.LIST<? Super Hashmap>)

Any method with one parameter, and the passed parameter type is a generic parameter, the generic parameter type is the base type of hashmap, such as public voi test (map map);Spring AOP is currently not working properly

* * (*< @cn. Javass. Secure *>)

Any method with one parameter, and the parameter type is a generic parameter that holds @cn.javass on the generic parameter type: secure annotations;Spring AOP testing is not working properly


B, within: Use "within (type expression)" to match the execution of methods within the specified type;

Mode

Description

Within (Cn.javass. *)

Any method execution under Cn.javass package and sub-package

Within (Cn.javass. ipointcutservice+)

Cn.javass any method of Ipointcutservice type and subtype under a package or all sub-packages

Within (@cn. Javass. Secure *)

Hold Cn.javass. Any method of any type of secure annotation must be declared on the target object, which does not work on the interface.



C, this:Use this (type fully qualified name) to match the execution method of the current AOP proxy object type, and note that the AOP proxy object type matches, which may include the introduction of an interface method or a match; note that the expression used in this must be of the type fully qualified name, and wildcard characters are not supported;

Mode

Description

This (Cn.javass.spring.chapter6.service.IPointcutService)

The current AOP object implements any method of the Ipointcutservice interface

This (Cn.javass.spring.chapter6.service.IIntroductionService)

Any method by which the current AOP object implements the Iintroductionservice interface may also be an introduction interface


D, Target:Use "target (type fully qualified name)" to match the execution method of the current target object type, note that the target object type matches, which does not include the introduction of the interface also type matching; Note that the expression used in target must be of the type fully qualified name, and wildcard characters are not supported;

Mode

Description

Target (Cn.javass.spring.chapter6.service.IPointcutService)

Any method that implements the Ipointcutservice interface for the current target object (non-AOP object)

Target (Cn.javass.spring.chapter6.service.IIntroductionService)

Any method that implements the Iintroductionservice interface for the current target object (non-AOP object) cannot be an ingestion interface


E,args: Use the "args (parameter type list)" to match the currently executing method to the execution method of the specified type, note that it matches the passed parameter type, is not a parameter type that matches the method signature, the parameter in the parameter type list must be of the type fully qualified name, the wildcard character does not support ; args belongs to the dynamic pointcut, which is very expensive and should not be used in non-special cases;

Mode

Description

Args (java.io.Serializable,..)

Any one that starts with an "incoming parameter type of java.io.Serializable" and can follow a method of any of the arguments of any type, the parameter type specified by args is dynamically matched at run time.

 

F, @within: use "@within (note type)" Match so hold the method within the specified annotation type, and the annotation type must be a fully qualified type name;

Mode

Description

@within cn.javass.spring.chapter6.Secure)

The type that corresponds to any target object holds the class method of secure annotations;

This annotation must be declared on the target object, which is declared on the interface and does not work on it.

 

g, @target: use @target (annotation type) to match the execution method of the current target object type, where the target object holds the specified annotation, and the annotation type must be a fully qualified type name;

Mode

Description

@target (Cn.javass.spring.chapter6.Secure)

Any target object that holds the class method for secure annotations;

This annotation must be declared on the target object, which is declared on the interface and does not work on it.

 

h, @args: use "@args (note list)" to match the currently executing method the passed parameters hold the execution of the specified annotation, and the annotation type must be a fully qualified type name;

Mode

Description

@args (Cn.javass.spring.chapter6.Secure)

Any one method that accepts only one parameter, and the arguments passed in when the method is run hold annotation cn.javass.spring.chapter6.Secure; a dynamic pointcut, similar to the ARG indicator;

 

I, @annotation: use @annotation (annotation type) to match the method in which the current execution method holds the specified annotation, and the annotation type must be a fully qualified type name;

Mode

Description

@annotation (Cn.javass.spring.chapter6.Secure)

The current execution method holds annotations Cn.javass.spring.chapter6.Secure will be matched

 

J, Bean: use "bean (Bean ID or name wildcard)" to match the specific name of the Bean object execution method; Spring ASP extended, there is no corresponding concept in ASPECTJ;

Mode

Description

Bean (*service)

Match all beans that end with service naming (ID or name)

K, reference pointcut:Indicates references to other named Pointcuts, only @apectj style support, schema style is not supported, as follows:

For example, we define the following facets:

Java Code

1 package CN.JAVASS.SPRING.CHAPTER6.AOP;  
2 import Org.aspectj.lang.annotation.Aspect;  
3 Import org.aspectj.lang.annotation.Pointcut;  
4 @Aspect  
5 public class Referencepointcutaspect {  
6     @Pointcut (value= "Execution (* * ())")  
7     public void Pointcut () {}  
8}  

You can refer to it in the following ways:

Java Code

1 @Before (value = "cn.javass.spring.chapter6.aop.ReferencePointcutAspect.pointcut ()")  
2 public void ReferencePointcutTest2 (Joinpoint JP) {}  

In addition to being able to reference within the @aspectj-style facets, you can also reference it in the schema-style facet definition, in the same way as @aspectj.

Here we have our pointcut expression syntax examples, we have almost everything in the daily development, but of course there are more complicated syntax and so on, if the above does not meet your needs, please refer to the ASPECTJ documentation.

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.