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.
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.
Spring AOP 仅仅支持Bean里面的方法切入点,所以切入点主要作用就是申明匹配Bean里面的方法。Pointcut 申明有两个部分哦,一个是签名方法 包含有方法名字和参数。另外一个是切入点表达式,这个是用来申明在哪些方法上面执行切入。例如:
@Pointcut("execution(* com.yuan.service..ad*(..))")
public void daddff(){};
/***
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).
pointcut 签名 必须是无返回值得。
下面就是一个例子:
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'
:
@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.
来源:oschina
链接:https://my.oschina.net/u/2308739/blog/397246