我使用AspectJ创建了一个自定义注释,并尝试创建注释,以便在注释无效时跳过方法。
但是我的方法体在任何情况下都不会被执行
这是我的代码
它是一种约束
package com.test;
public @interface SwitchLiteConstraint {
Class<SwitchLiteValidator>[] validatedBy();
}下面是约束验证器
package com.test;
import java.lang.annotation.Annotation;
public interface SwitchLiteConstraintValidator<T1 extends Annotation, T2> {
void initialize(T1 annotation);
boolean isValid(T2 value, SwitchLiteConstraintValidatorContext validatorContext);
}下面是我的方面类
package com.test;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class SwitchLiteValidationAspect {
@Around("execution(boolean *(..))")
public boolean skipValidation(ProceedingJoinPoint thisJoinPoint) throws Throwable {
Method method = MethodSignature.class.cast(thisJoinPoint.getSignature()).getMethod();
SwitchLiteAnnotation puff = method.getAnnotation(SwitchLiteAnnotation.class);
if (puff == null) {
System.out.println(" puff null returning true ");
return true;
} else {
String aspectName = puff.message().trim();
if(aspectName.equalsIgnoreCase("msg")){
System.out.println(" returning true ");
return true;
}else{
System.out.println(" returning false ");
return false;
}
}
}
}下面是验证器的实现
package com.test;
public class SwitchLiteValidator implements SwitchLiteConstraintValidator<SwitchLiteAnnotation, String> {
@Override
public void initialize(SwitchLiteAnnotation annotation) {}
@Override
public boolean isValid(String value, SwitchLiteConstraintValidatorContext validatorContext) {
if ("msg".equalsIgnoreCase(value)){
//return true;
return true;
}
//return false;
return false;
}
}下面是我的自定义注解
package com.test;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.*;
@Target({ METHOD, FIELD, PARAMETER })
@Retention(RUNTIME)
@SwitchLiteConstraint(validatedBy = { SwitchLiteValidator.class })
public @interface SwitchLiteAnnotation {
String message() default "DEFAULT_FALSE";
}下面是示例,我使用注解的方式
package com.test;
public class Application {
public static void main(String[] args) {
Application application = new Application();
boolean first=false;
boolean second=false;
first=application.validate1();
second=application.validate2();
System.out.println(first);
System.out.println(second);
}
@SwitchLiteAnnotation(message = "abc")
public boolean validate1() {
System.out.println("validate1");
return true;
}
@SwitchLiteAnnotation(message = "msg")
public boolean validate2() {
System.out.println("validate2");
return true;
}
}问题是我的System.out.println("validate1");和System.out.println("validate2");正在被执行
发布于 2017-03-17 19:01:26
您的注解SwitchLiteConstraint需要在运行时保留,否则它在运行时不可见,因此对方面也不可见。你可能只是忘了这一点。
更新:我复制了你所有的源代码,并在我的电脑上试用。我不明白你的问题是什么。如果没有方面,则输出为:
validate1
validate2
true
true有了方面,输出变成了:
returning false
returning true
false
true这不是你想要的吗?
https://stackoverflow.com/questions/42850782
复制相似问题