首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@aspect -使用的@aspect- @around注解-未执行自定义注解方法

@aspect -使用的@aspect- @around注解-未执行自定义注解方法
EN

Stack Overflow用户
提问于 2017-03-17 14:37:11
回答 1查看 450关注 0票数 0

我使用AspectJ创建了一个自定义注释,并尝试创建注释,以便在注释无效时跳过方法。

但是我的方法体在任何情况下都不会被执行

这是我的代码

它是一种约束

代码语言:javascript
复制
package com.test;
public @interface SwitchLiteConstraint {

  Class<SwitchLiteValidator>[] validatedBy();

}

下面是约束验证器

代码语言:javascript
复制
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);
}

下面是我的方面类

代码语言:javascript
复制
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;
            }
        }

    }
}

下面是验证器的实现

代码语言:javascript
复制
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;
  }
}

下面是我的自定义注解

代码语言:javascript
复制
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";
}

下面是示例,我使用注解的方式

代码语言:javascript
复制
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");正在被执行

EN

回答 1

Stack Overflow用户

发布于 2017-03-17 19:01:26

您的注解SwitchLiteConstraint需要在运行时保留,否则它在运行时不可见,因此对方面也不可见。你可能只是忘了这一点。

更新:我复制了你所有的源代码,并在我的电脑上试用。我不明白你的问题是什么。如果没有方面,则输出为:

代码语言:javascript
复制
validate1
validate2
true
true

有了方面,输出变成了:

代码语言:javascript
复制
  returning false 
  returning true 
false
true

这不是你想要的吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42850782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档