我有注释@Validatable
,我希望拦截对该方法的所有调用,返回int
。对于intsance:
@Validatable
public int method(){
//...
}
如何编写pointcut
来做到这一点?一般来说,我需要编写以下方面:
public aspect ValidateAspect {
pointcut publicMethodExecuted(): execution(__HERE_SHOULD_BE_THE_PATTERN__);
int around() : publicMethodExecuted() {
//performing some validation and changing return value
}
}
答案 0 :(得分:0)
使用以下代码获取属于int method()
方法的注释后,您可以执行所需的操作:
pointcut publicMethodExecuted(): execution(public int <classname>.method());
int around() : publicMethodExecuted() {
//performing some validation and changing return value
MethodSignature signature = (MethodSignature) thisJoinPoint.getSignature();
String methodName = signature.getMethod().getName();
Annotation[] annotations = thisJoinPoint.getThis().getClass().getDeclaredMethod(methodName).getAnnotations();
for (Annotation annotation : annotations)
System.out.println(annotation);
}
答案 1 :(得分:0)
AspectJ支持非常简单的PointCut Designators用于带注释的方法。对于您的用例,它是:
public aspect ValidateAspect {
pointcut publicMethodExecuted(): @annotation(Validatable);
int around() : publicMethodExecuted() {
//performing some validation and changing return value
}
}