用于获取参数和注释值的切入点表达式

时间:2017-08-16 17:38:05

标签: spring aop spring-aop pointcut

我有一个切入点表达式,可以调用包中的所有方法。

某些方法可能包含注释和需要获取建议的参数。

我试过这样的事情

@Around("execution(* com.man.test..jmx..*(..)) && args(name,..) && @annotation( requiredJMX )")

这个表达式的问题在于,如果存在名称和注释的参数,它将调用。

我是否可以调用包中的所有方法,同时name参数和注释是可选的?

像这样的东西

@Around("execution(* com.man.test..jmx..*(..)) || args(name,..) || @annotation( requiredJMX )")

1 个答案:

答案 0 :(得分:1)

如果你想从方法中获取注释和args的值, 你可以这样做:

@Around("execution(* com.man.test..jmx..*(..)) public Object yourMethod(ProceedingJoinPoint joinpoint){

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
if(null != annotation && method.getParameters().length > 0 
     && "name".equals(method.getParameters()[0].getName())){

    //do your work
}

}

相关问题