切入点用来匹配在哪些连接点(Spring AOP中只是方法执行)上执行通知(请参考Spring AOP方面的资料)。
切入点定义格式如下:
execution (modifiers-pattern? ret-type declaring-type? name-pattern (param-pattern) throws-pattern?)
其中方法返回值(ret-type)和方法名(name-pattern),参数(param-pattern)必选,其他为可选。
返回类型常用*,表示任意返回类型;也可用*匹配名字模式,表示任意名字的方法;
参数匹配方式有几种方式:
1.()匹配一个不接受参数的方法;
2.(..)匹配一个接受任意多个参数的方法(0个或多个);
3.(*)匹配一个接受一个任意类型参数的方法;
4.(*,String)匹配一个接受一个任意类型参数和一个String类型参数的方法;
以下是一些切入点表达式例子(记录于Spring官方文档)
一、任意公共方法的执行
execution (public * *(..))
二、任何一个名字以“set”开头的方法执行
execution (* set*(..))
三、AccountService接口定义的任意方法的执行
execution (* com.rote.AccountService.*(..))
四、在service包中定义的任意方法的执行
execution (* com.rote.service.*.*(..))
五、在service包及其子包中定义的任意方法的执行
execution (* com.rote.service..*.*(..))
六、在service包中的任意连接点(Spring AOP中只是指方法执行)
within (com.rote.service.*)
七、在service包中及其子包中定义的任意连接点(Spring AOP中只是指方法执行)
within (com.rote.service..*)
八、实现了AccountService接口的代理对象的任意连接点(Spring AOP中只是指方法执行)
this (com.rote.AccountService)
九、实现了AccountService接口的目标对象的任意连接点(Spring AOP中只是指方法执行)
target (com.rote.AccountService)
十、任何一个只接受一个参数,且运行时所需传入的参数是Serializable接口的连接点(Spring AOP中只是指方法执行)
args (java.io.Serializable)
第十点中的定义和execution (* *(java.io.Serializable)) 不同;args只有在动态运行时传入参数是Serializable时匹配,execution在方法签名中声明只有一个Serializable类型的参数时匹配