2017-03-17 18 views
0

アノテーションを使用してカスタムアノテーションを作成し、アノテーションが有効でない場合にメソッドをスキップするのに役立つアノテーションを作成しようとしました。@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");実行中

答えて

0

注釈SwitchLiteConstraintは実行時の保持を必要とします。そうでなければ、実行時には表示されず、したがって視覚的に見えません。あなたはおそらくそれを忘れてしまったでしょう。


アップデート:私はすべてのソースコードをコピーして自分のコンピュータ上で試してみました。私はあなたの問題が何であるか理解していません。側面がなければ、出力は次のようになります。出力になる態様によれば

validate1 
validate2 
true 
true 

returning false 
    returning true 
false 
true 

は、あなたが望んでいないものをということですか?

+0

私はあなたのアプローチで試しましたが、まだメソッド本体が実行されていません –

+0

注釈の変更内容を反映するように、あなたの質問のコードを更新してください。どうもありがとうございました。 – kriegaex

+0

あなたのコードをテストしました。私の更新を見てください。それで問題は何ですか?ところで、あなたの面は本当に工夫されていて、簡単に書くことができます。 – kriegaex

関連する問題