Java 如何拦截@validable注释的所有方法
我有注释Java 如何拦截@validable注释的所有方法,java,aspectj,Java,Aspectj,我有注释@validable,我想截获所有对方法的调用,该注释返回int。对于intsance: @Validatable public int method(){ //... } 如何编写切入点?一般来说,我需要写以下方面: public aspect ValidateAspect { pointcut publicMethodExecuted(): execution(__HERE_SHOULD_BE_THE_PATTERN__); int around() : p
@validable
,我想截获所有对方法的调用,该注释返回int
。对于intsance:
@Validatable
public int method(){
//...
}
如何编写切入点?一般来说,我需要写以下方面:
public aspect ValidateAspect {
pointcut publicMethodExecuted(): execution(__HERE_SHOULD_BE_THE_PATTERN__);
int around() : publicMethodExecuted() {
//performing some validation and changing return value
}
}
使用以下代码获得属于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);
}
切入点publicMethodExecuted():执行(public int.method());
int around():publicMethodExecuted(){
//执行一些验证并更改返回值
MethodSignature=(MethodSignature)thisJoinPoint.getSignature();
String methodName=signature.getMethod().getName();
Annotation[]annotations=thisJoinPoint.getThis().getClass().getDeclaredMethod(methodName.getAnnotations();
用于(注释:注释)
System.out.println(注释);
}
AspectJ非常简单地支持带注释方法的切入点指示符。对于您的用例,它是:
public aspect ValidateAspect {
pointcut publicMethodExecuted(): @annotation(Validatable);
int around() : publicMethodExecuted() {
//performing some validation and changing return value
}
}