2017-03-17 202 views
0

我使用AspectJ創建了一個自定義註釋,並嘗試創建註釋,以便在註釋無效時幫助跳過方法。@aspect - @around註釋 - 使用的自定義註釋方法不被執行

但我的方法的身體沒有得到

在此執行任何情況下,它是我的代碼

它是約束

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」);正在執行

回答

0

您的註釋SwitchLiteConstraint需要運行時保留,否則它將在運行時不可見並因此對方面不可見。你可能只是忘了那個。


更新:我複製你的所有的源代碼,並試圖在我的電腦上。我不明白你的問題是什麼。如果沒有方面的輸出是:

validate1 
validate2 
true 
true 

隨着輸出變成方面:

returning false 
    returning true 
false 
true 

是你不想要的東西?

+0

我試過你的方法,但方法體仍未執行 –

+0

請你更新你的問題中的代碼,以反映你在註釋中改變了什麼?非常感謝你。 – kriegaex

+0

我測試了你的代碼,看看我的更新。那麼問題是什麼?順便說一句,你的方面真的是人爲的,它可以寫得更簡單。 – kriegaex