spring aop 申明瞭切面類以後,如何申明切入點呢?

8.2.3 Declaring a pointcut

Recall that pointcuts determine join points of interest, and thus enable us to control when advice executes. Spring AOP only supports method execution join points for Spring beans, so you can think of a pointcut as matching the execution of methods on Spring beans.html

 A pointcut declaration has two parts: a signature comprising a name and any parameters, and a pointcut expression that determines exactly which method executions we are interested in.express

Spring AOP 僅僅支持Bean裏面的方法切入點,因此切入點主要做用就是申明匹配Bean裏面的方法。Pointcut 申明有兩個部分哦,一個是簽名方法 包含有方法名字和參數。另一個是切入點表達式,這個是用來申明在哪些方法上面執行切入。例如:eclipse

@Pointcut("execution(* com.yuan.service..ad*(..))")ide

    public void daddff(){};ui

    /***this

 In the @AspectJ annotation-style of AOP, a pointcut signature is provided by a regular method definition, and the pointcut expression is indicated using the @Pointcut annotation (the method serving as the pointcut signature must have a void return type).spa

pointcut 簽名 必須是無返回值得。rest

下面就是一個例子:code

An example will help make this distinction between a pointcut signature and a pointcut expression clear. The following example defines a pointcut named'anyOldTransfer' that will match the execution of any method named 'transfer':orm

@Pointcut("execution(* transfer(..))")// the pointcut expression
private void anyOldTransfer() {}     // the pointcut signature

The pointcut expression that forms the value of the @Pointcut annotation is a regular AspectJ 5 pointcut expression. For a full discussion of AspectJ’s pointcut language, see the AspectJ Programming Guide (and for extensions, the AspectJ 5 Developers Notebook) or one of the books on AspectJ such as "Eclipse AspectJ" by Colyer et. al. or "AspectJ in Action" by Ramnivas Laddad.

注意:pointcut expression

Spring AOP users are likely to use the execution pointcut designator the most often. The format of an execution expression is:

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

All parts except the returning type pattern (ret-type-pattern in the snippet above), name pattern, and parameters pattern are optional. The returning type pattern determines what the return type of the method must be in order for a join point to be matched. Most frequently you will use * as the returning type pattern, which matches any return type. A fully-qualified type name will match only when the method returns the given type. The name pattern matches the method name. You can use the * wildcard as all or part of a name pattern. The parameters pattern is slightly more complex: () matches a method that takes no parameters, whereas (..)matches any number of parameters (zero or more). The pattern (*) matches a method taking one parameter of any type, (*,String) matches a method taking two parameters, the first can be of any type, the second must be a String. Consult the Language Semantics section of the AspectJ Programming Guide for more information.

Some examples of common pointcut expressions are given below.

相關文章
相關標籤/搜索