@Pointcut语法详解

https://www.mekau.com/4880.html
https://blog.csdn.net/xiaoyiaoyou/article/details/45972363

定义

格式:@ 注解(value=“表达标签 ( 表达式格式)”)
如:@Pointcut (value=“execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(…))”)

表达式标签

  • execution():用于匹配方法执行的连接点
  • args(): 用于匹配当前执行的方法传入的参数为指定类型的执行方法
  • this(): 用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配;
  • target(): 用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;
  • within(): 用于匹配指定类型内的方法执行;
  • @args():于匹配当前执行的方法传入的参数持有指定注解的执行;
  • @target():用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;
  • @within():用于匹配所以持有指定注解类型内的方法;
  • @annotation:用于匹配当前执行方法持有指定注解的方法;

其中execution 是用的最多的,

execution

execution格式:

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

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
其中带 ?号的 modifiers-pattern?,declaring-type-pattern?,hrows-pattern?是可选项
ret-type-pattern,name-pattern, parameters-pattern是必选项;

  • modifier-pattern? 修饰符匹配,如public 表示匹配公有方法
  • ret-type-pattern 返回值匹配,* 表示任何返回值,全路径的类名等
  • declaring-type-pattern? 类路径匹配
  • name-pattern 方法名匹配,* 代表所有,set*,代表以set开头的所有方法
  • (param-pattern) 参数匹配,指定方法参数(声明的类型),
    (…)代表所有参数,
    ()代表一个参数,
    (
    ,String)代表第一个参数为任何值,第二个为String类型.
  • throws-pattern? 异常类型匹配

例子:

  • execution(public * *(..)) 定义任意公共方法的执行
  • execution(* set*(..)) 定义任何一个以"set"开始的方法的执行
  • execution(* com.xyz.service.AccountService.*(..)) 定义AccountService 接口的任意方法的执行
  • execution(* com.xyz.service.*.*(..)) 定义在service包里的任意方法的执行
  • execution(* com.xyz.service ..*.*(..)) 定义在service包和所有子包里的任意类的任意方法的执行
  • execution(* com.test.spring.aop.pointcutexp…JoinPointObjP2.*(…)) 定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
    (..),.(..)),JoinPointObjP2.(..))\color{red}{说明:最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))}

AspectJ类型匹配的通配符:

  • * \color{blue}{匹配任何数量字符;}
  • . . \color{blue}{匹配任何数量字符的重复,如在类型模式中匹配任何数量子包;而在方法参数模式中匹配任何数量参数。}
  • +:\color{blue}{匹配指定类型的子类型;仅能作为后缀放在类型模式后边。}

如:

  • java.lang.String 匹配String类型;
  • java.*.String 匹配java包下的任何“一级子包”下的String类型;如匹配java.lang.String,但不匹配java.lang.ss.String
  • java..* 匹配java包及任何子包下的任何类型; 如匹配java.lang.String、java.lang.annotation.Annotation
  • java.lang.*ing 匹配任何java.lang包下的以ing结尾的类型;
  • java.lang.Number+ 匹配java.lang包下的任何Number的自类型;如匹配java.lang.Integer,也匹配java.math.BigInteger

within和@within

  • within(com.test.spring.aop.pointcutexp.*) pointcutexp包里的任意类.
  • within(com.test.spring.aop.pointcutexp…*) pointcutexp包和所有子包里的任意类.
  • @within(org.springframework.transaction.annotation.Transactional) 带有@Transactional标注的所有类的任意方法.

this

  • this(com.test.spring.aop.pointcutexp.Intf) 实现了Intf接口的所有类,如果Intf不是接口,限定Intf单个类.

AOP,getBeancast,.\color{red}{当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.}

@target

  • @target(org.springframework.transaction.annotation.Transactional) 带有@Transactional标注的所有类的任意方法.

@annotation

  • @annotation(org.springframework.transaction.annotation.Transactional) 带有@Transactional标注的任意方法.
    @within@target,@annotation\color{red}{@within和@target针对类的注解,@annotation是针对方法的注解}

args 和 @args

  • @args(org.springframework.transaction.annotation.Transactional) 参数带有@Transactional标注的方法.
  • args(String) 参数为String类型(运行是决定)的方法.

命名及匿名切入点

Pointcut,使&&!\color{red}{Pointcut定义时,还可以使用\&\&、 || 、! 运算符}

@Pointcut("execution(* com.savage.aop.MessageSender.*(..)) && args(param)")
public void log(){
}
@Before("log(String param)") 
 public void beforeLog(){
     //todo something....
 }
 //等同于
@Before("execution(* com.savage.aop.MessageSender.*(..)) && args(param)") 
 public void beforeLog(){
     //todo something....
 }

又如:

@Pointcut("execution(* com.savage.aop.MessageSender.*(..))")
    private void logSender(){}

    @Pointcut("execution(* com.savage.aop.MessageReceiver.*(..))")
    private void logReceiver(){}

    @Pointcut("logSender() || logReceiver()")
    private void logMessage(){} 

这个例子中,logMessage()将匹配任何MessageSender和MessageReceiver中的任何方法

切入点使用示例:

  • 一、execution:使用“execution(方法表达式)”匹配方法执行;
示例 描述
public * *(…) 任何公共方法的执行
* cn.javass…IPointcutService.*() cn.javass包及所有子包下IPointcutService接口中的任何无参方法
* cn.javass….(…) cn.javass包及所有子包下任何类的任何方法
* cn.javass…IPointcutService.() cn.javass包及所有子包下IPointcutService接口的任何只有一个参数方法
* (!cn.javass…IPointcutService+).*(…) 非“cn.javass包及所有子包下IPointcutService接口及子类型”的任何方法
* cn.javass…IPointcutService+.*() cn.javass包及所有子包下IPointcutService接口及子类型的的任何无参方法
* cn.javass…IPointcut*.test*(java.util.Date) cn.javass包及所有子包下IPointcut前缀类型的的以test开头的只有一个参数类型为java.util.Date的方法,注意该匹配是根据方法签名的参数类型进行匹配的,而不是根据执行时传入的参数类型决定的,如定义方法:public void test(Object obj);即使执行时传入java.util.Date,也不会匹配的;
* cn.javass…IPointcut*.test*(…) throws IllegalArgumentException,ArrayIndexOutOfBoundsException,cn.javass 包及所有子包下IPointcut前缀类型的的任何方法,且抛出IllegalArgumentException和ArrayIndexOutOfBoundsException异常
* (cn.javass…IPointcutService+&& java.io.Serializable+).*(…) 任何实现了cn.javass包及所有子包下IPointcutService接口和java.io.Serializable接口的类型的任何方法
@java.lang.Deprecated * *(…) 任何持有@java.lang.Deprecated注解的方法
@java.lang.Deprecated @cn.javass…Secure * *(…) 任何持有@java.lang.Deprecated和@cn.javass…Secure注解的方法
@(java.lang.Deprecated
(@cn.javass…Secure *) *(…) 任何返回值类型持有@cn.javass…Secure的方法
* (@cn.javass…Secure ).(…) 任何定义方法的类型持有@cn.javass…Secure的方法
* (@cn.javass…Secure () , @cn.javass…Secure (*)) 任何签名带有两个参数的方法,且这个两个参数都被@ Secure标记了,如public void test(@Secure String str1, @Secure String str1);
* *((@ cn.javass…Secure ))或 *(@ cn.javass…Secure *) 任何带有一个参数的方法,且该参数类型持有@ cn.javass…Secure;
如public void test(Model model);且Model类上持有@Secure注解
* *(@cn.javass…Secure (@cn.javass…Secure *) ,@ cn.javass…Secure (@cn.javass…Secure *)) 任何带有两个参数的方法,且这两个参数都被@ cn.javass…Secure标记了;且这两个参数的类型上都持有@ cn.javass…Secure;
* *(java.util.Map<cn.javass…Model, cn.javass…Model>, …) 任何带有一个java.util.Map参数的方法,且该参数类型是以< cn.javass…Model, cn.javass…Model >为泛型参数;注意只匹配第一个参数为java.util.Map,不包括子类型;如public void test(HashMap<Model, Model> map, String str);将不匹配,必须使用“* *(java.util.HashMap<cn.javass…Model,cn.javass…Model>, …)”进行匹配;而public void test(Map map, int i);也将不匹配,因为泛型参数不匹配
* *(java.util.Collection<@cn.javass…Secure *>) 任何带有一个参数(类型为java.util.Collection)的方法,且该参数类型是有一个泛型参数,该泛型参数类型上持有@cn.javass…Secure注解;如public void test(Collection collection);Model类型上持有@cn.javass…Secure
* *(java.util.Set<? extends HashMap>) 任何带有一个参数的方法,且传入的参数类型是有一个泛型参数,该泛型参数类型继承与HashMap;Spring AOP目前测试不能正常工作
* *(java.util.List<? super HashMap>) 任何带有一个参数的方法,且传入的参数类型是有一个泛型参数,该泛型参数类型是HashMap的基类型;如public voi test(Map map);Spring AOP目前测试不能正常工作
* (<@cn.javass…Secure *>) 任何带有一个参数的方法,且该参数类型是有一个泛型参数,该泛型参数类型上持有@cn.javass…Secure注解;Spring AOP目前测试不能正常工作
  • 二、within:使用“within(类型表达式)”匹配指定类型内的方法执行;
示例 描述
within(cn.javass…*) cn.javass包及子包下的任何方法执行
within(cn.javass…IPointcutService+) cn.javass包或所有子包下IPointcutService类型及子类型的任何方法
within(@cn.javass…Secure *) 持有cn.javass…Secure注解的任何类型的任何方法必须是在目标对象上声明这个注解,在接口上声明的对它不起作用
  • 三、this:使用“this(类型全限定名)”匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口方法也可以匹配;注意this中使用的表达式必须是类型全限定名,不支持通配符;
示例 描述
this(cn.javass.spring.chapter6.service.IPointcutService) 当前AOP对象实现了 IPointcutService接口的任何方法
this(cn.javass.spring.chapter6.service.IIntroductionService) 当前AOP对象实现了 IIntroductionService接口的任何方法
  • 四、target:使用“target(类型全限定名)”匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;注意target中使用的表达式必须是类型全限定名,不支持通配符;
示例 描述
target(cn.javass.spring.chapter6.service.IPointcutService) 当前目标对象(非AOP对象)实现了 IPointcutService接口的任何方法
target(cn.javass.spring.chapter6.service.IIntroductionService) 当前目标对象(非AOP对象) 实现了IIntroductionService 接口的任何方法, 不可能是引入接口
  • 五、args:使用“args(参数类型列表)”匹配当前执行的方法传入的参数为指定类型的执行方法;注意是匹配传入的参数类型,不是匹配方法签名的参数类型;参数类型列表中的参数必须是类型全限定名,通配符不支持;args属于动态切入点,这种切入点开销非常大,非特殊情况最好不要使用;
示例 描述
args (java.io.Serializable,…) 任何一个以接受“传入参数类型为 java.io.Serializable” 开头,且其后可跟任意个任意类型的参数的方法执行,args指定的参数类型是在运行时动态匹配的
  • 六、@within:使用“@within(注解类型)”匹配所以持有指定注解类型内的方法;注解类型也必须是全限定类型名;
示例 描述
@within(cn.javass.spring.chapter6.Secure) 任何目标对象对应的类型持有Secure注解的类方法;
  • 七、@target:使用“@target(注解类型)”匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;注解类型也必须是全限定类型名;
示例 描述
@target (cn.javass.spring.chapter6.Secure) 任何目标对象持有Secure注解的类方法;必须是在目标对象上声明这个注解,在接口上声明的对它不起作用
  • 八、@args:使用“@args(注解列表)”匹配当前执行的方法传入的参数持有指定注解的执行;注解类型也必须是全限定类型名;
示例 描述
@args (cn.javass.spring.chapter6.Secure) 任何一个只接受一个参数的方法,且方法运行时传入的参数持有注解 cn.javass.spring.chapter6.Secure;动态切入点,类似于arg指示符;
  • 九、@annotation:使用“@annotation(注解类型)”匹配当前执行方法持有指定注解的方法;注解类型也必须是全限定类型名;
示例 描述
@annotation(cn.javass.spring.chapter6.Secure ) 当前执行方法上持有注解 cn.javass.spring.chapter6.Secure将被匹配
  • 十、bean:使用“bean(Bean id或名字通配符)”匹配特定名称的Bean对象的执行方法;Spring ASP扩展的,在AspectJ中无相应概念;
示例 描述
bean(*Service) 匹配所有以Service命名(id或name)结尾的Bean
  • 十一、reference pointcut:表示引用其他命名切入点;
@Pointcut(value="bean(*Service)")
private void pointcut1(){
}
@Pointcut(value="@args()cn.javass.spring.chaper6.Source")
public referencePointcutTest(JoinPoint jp){
     ...
}

AOP例子

@Aspect
@Component
public class NotVeryUsefulAspect {
	@AfterReturning(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
	private void logReceiver(){
		System.out.println("切入点logReceiver...");
	}

	@Pointcut(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..)) && args(param)")
	private void pointcut(String param){ 
		System.out.println("切入点pointcut()"+param);
	}

	//方法体将不执行
	@Pointcut("within(com.cn.spring.aspectj.*)")
	public String inWebLayer() {
		System.out.println("切入点inWebLayer()");
		return "返回值加载";
	}

	@Before(value="inWebLayer()")
		private void beforeinWebLayer(){ 
		System.out.println("beforeinWebLayer~~");
	}

	@Before(value="pointcut(param)")
		private void beforePointcut(String param){ 
		System.out.println("beforePointcut:"+param);
	}


	@AfterReturning(pointcut="inWebLayer()",returning="retVal")
		public void doAccessCheck(Object retVal) {
		System.out.println("doAccessCheck:"+retVal);
	}

	@Around(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
	private Object aroundLayer(ProceedingJoinPoint pjp) throws Throwable{ 
		// start stopwatch
		Object retVal = pjp.proceed();
		// stop stopwatch
		System.out.println("aroundLayer~~");
		return retVal;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章