アノテーションを使用してカスタムアノテーションを作成し、アノテーションが有効でない場合にメソッドをスキップするのに役立つアノテーションを作成しようとしました。@aspect - @aroundアノテーションを使用 - カスタムアノテートメソッドが実行されない
しかし、私のメソッド本体が
ここでどのような場合に実行取得されていない、それは以下の
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;
public @interface SwitchLiteConstraint {
Class<SwitchLiteValidator>[] validatedBy();
}
である制約バリデータに私のコード
です私のアスペクトクラスです
下記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;
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;
}
}
使用しています途中、私のカスタム注釈
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";
}
以下は一例であるです問題は私のSystem.out.println( "validate1")です。とSystem.out.println( "validate2");実行中
私はあなたのアプローチで試しましたが、まだメソッド本体が実行されていません –
注釈の変更内容を反映するように、あなたの質問のコードを更新してください。どうもありがとうございました。 – kriegaex
あなたのコードをテストしました。私の更新を見てください。それで問題は何ですか?ところで、あなたの面は本当に工夫されていて、簡単に書くことができます。 – kriegaex