Spring AOP with groovy: get called method

此生再无相见时 提交于 2019-12-04 21:02:53

For Option 1: Try adding a !getMetaClass() Pointcut to your @Aspect class like this:

@Pointcut("!execution(* mypackage.MyService.*.getMetaClass(..))")
public void noMetaClassMethods() {}

As well as making your original execution matcher into a Pointcut:

@Pointcut("execution(* mypackage.MyService.*(..))")
public void myServices() {}

Then combine those two in your @Before like this:

@Before("myServices() && noMetaClassMethods()")
void beforeMethod(JoinPoint joinPoint) {
    logger.info(joinPoint.signature.name + " called")
}

It should give you what you're looking for.

For Option 2: You could add a name attribute to your annotation on the target method:

@Timed(name="methodIWantToTime")
def methodIWantTime(..)

Then just include the annotation as an argument your method in your Aspect class:

@Around(value="@annotation(timed)")
def timeMethod(ProceedingJoinPoint proceedingJointPoint, Timed timed) {
    println timed.name()
    proceedingJointPoint.proceed()
}

and peel it off of that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!