在春天创建类似于@Transaction的方面

时间:2014-06-18 02:28:04

标签: java spring aspectj

我正在阅读this链接,但我有疑问。在这里,我们定义了方面将被调用的位置,例如@Pointcut("execution(* com.tutorialspoint.*.*(..))")。但是,如果我们要激发我明确提到方面或注释的方面,你会怎么做?例如,在类的特定方法中,如果我们使用某个注释进行注释,则应该在spring中调用类似于@Transaction的特定方面。怎么做类似的事情。感谢。

1 个答案:

答案 0 :(得分:0)

您可能感兴趣的有两个可注释的点:类和/或方法。

如果您对具有特定注释的班级感兴趣,可以使用@within(SomeAnnotationType)within(@SomeAnnotationType *)。将其与执行相结合,挑选出带注释的类型execution(* *(..)) && @within(SomeAnnotationType)

中任何方法的执行

如果您对要注释的特定方法感兴趣,可以使用@Pointcut("execution(@SomeAnnotationType * *.*(..))")。在这种情况下,类型是否注释无关紧要。请参阅方法签名前面的注释。

你当然可以将这两者结合起来,就像Spring一样:

pointcut executionOfMethodInAnnotatedType:
  execution(* *(..)) && @within(MyAnnotation);

pointcut executionOfAnnotatedMethod():
  execution(@MyAnnotation * *(..));

pointcut executionOfInterestingMethod():
  executionOfMethodInAnnotatedType() ||
  executionOfAnnotatedMethod();