Spring's AOP aspectj pointcut syntax (most of all, no need to go to another place to find)---Zhangkaitao

Source: Internet
Author: User
Tags anonymous aop deprecated
Spring AOP aspectj pointcut Syntax (most of all, no need to go to another place to find)http://jinnianshilongnian.iteye.com/blog/1415606--zhangkaitao

6.5 AspectJ Point of entry grammar detailed 6.5.1 Spring AOP-supported ASPECTJ pointcut indicator

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. 6.5.1 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:

6.5.2; 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.

 


Java code: View Copy to clipboard print 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:                     * Match any type under the Java package and any sub-packages;  such as Match java.lang.String, java.lang.annotation.Annotation java.lang.*ing match any Java.lang package under the type of ING end; 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:

Match type: match using the following method

Java code: View Copy to clipboard print annotations. The fully qualified name annotation of the class : Optional, annotations held on type, such as @deprecated; fully qualified name of class: required, can be any class fully qualified name.

Match method Execution: use the following method to match:

Java code: View Copy to clipboard print annotations. Modifier? Returns a value type type declaration? Method name (parameter list) exception list.

annotations: Optional, annotations held on methods, 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 has no 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 the end of a parameter that matches the accepted java.lang.String type, and a method that accepts an arbitrary type argument in front of it; exception list: optional, with the "throws exception fully qualified list" declaration, the list of exceptions fully qualified list if there are multiple "," split, such as throws Java.lang.IllegalArgumentException, Java.lang.ArrayIndexOutOfBoundsException.

Match Bean Name: You can use the Bean's ID or name to match, and you can use the wildcard character "*"; 6.5.3 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 &&, | | 、。。

6.5.3 Pointcut Use example

First, execution: use "Execution (method expression)" matching method execution;

 

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 against the parameter type of the method signature, rather than depending on the type of argument passed in at execution time.

As defined by: 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 the model class holds @secure annotations

* *(

@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 matching only the first parameter is Java.util.Map, excluding subtypes;

such as public void Test (Hashmap<model, model> map, String str), will not match, must use "* * * (

Java.util.hashmap<cn.javass. Model,cn.javass. Model>

, ..)” to match;

The public void test (map map, int i) will also 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, and the generic parameter type inherits from HashMap;

Spring AOP Testing is not working properly

* * (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 Testing is 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

 

two .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 for secure annotations

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

 

three . This: use "This (type fully qualified name)" 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 introducing an interface method that can also match Note that the expression used in this must be a fully-qualified type, and wildcard characters are not supported;

 

mode

description

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

The current AOP object implements the Ipointcutser Vice any method of the interface

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

Any method that implements the Iintroductionservice interface for the current AOP object

may also be an introduction interface

Related Article

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.