【问题标题】:annotation with parameter for aspect带有参数的注释
【发布时间】:2018-12-14 15:27:02
【问题描述】:

我有一个可用于注释的方面:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DumpToFile {

}

还有连接点:

@Aspect
@Component
public class DumpToFileAspect {

  @Around("@annotation(DumpToFile)")
  public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {

    ...
    // I likte to read out a parameter from the annotation...
    Object proceed = joinPoint.proceed();

    ...

    return proceed;
  }
}

我可以在@DumpToFile 的方法上成功使用方面;但是,我想将参数传递给注释并在我的方面检索它的值。
例如。 @DumpToFile(fileName="mydump")。谁能告诉我怎么做?

【问题讨论】:

    标签: java spring annotations aop aspectj


    【解决方案1】:

    您应该能够将注解接口传递给拦截器方法。不过我自己没试过。

    Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface DumpToFile {
    
          String fileName() default "default value";
    
    }
    

    在 DumpToFileAspect 中 -

    @Aspect
    @Component
    public class DumpToFileAspect {
    
      @Around("@annotation(dtf)")
      public Object logExecutionTime(ProceedingJoinPoint joinPoint, DumpToFile dtf) throws Throwable {
    
        ...
        // I likte to read out a parameter from the annotation...
    
        System.out.println(dtf.fileName); // will print "fileName"
    
        Object proceed = joinPoint.proceed();
    
        ...
    
        return proceed;
      }
    }
    

    【讨论】:

    • 谢谢!但是,我得到一个错误。 IntelliJ 已经用提示 Unbound pointcut parameter dtf' 将 @Around 标记为红色。如果我启动应用程序,我会收到异常 Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression
    • mm..不确定。你能仔细检查一下你是否正确导入了 org.aspectj.lang.JoinPoint..
    • import org.aspectj.lang.ProceedingJoinPoint;
    • 啊,我明白了。在您的示例中,它必须是 @Around("@annotation(dtf)") - 即它必须匹配参数的名称,而不是类型。 - 如果你能解决这个问题,我会接受答案。
    • 我修复了@shakhawat 的示例代码,以使 OP 能够接受答案。
    【解决方案2】:

    将您的 @Around 更改为:

    @Aspect
    @Component
    public class DumpToFileAspect {
    
      @Around("@annotation(dumpToFileAspect)")
      public Object logExecutionTime(ProceedingJoinPoint joinPoint, DumpToFile dumpToFileAspect) throws Throwable {
    
        ...
        // I likte to read out a parameter from the annotation...
        String fileName = dumpToFileAspect.getFileName();
        Object proceed = joinPoint.proceed();
    
        ...
    
        return proceed;
      }
    }
    

    【讨论】:

    • 这里的问题和我在shakhawat的回答中评论的一样
    • 尝试将其绑定到您的包,例如:@Around("execution(* com.your.package..*.*(..)) && @annotation(DumpToFile)")
    • 啊,我明白了。在您的示例中,它必须是 @Around("@annotation(dumpToFileAspect)") - 即它必须匹配参数的名称,而不是类型。我会接受shakhawat的答案,因为它更完整。
    • 我也修复了这里的代码。伙计们,请在发布答案之前测试您的代码,否则您只会激怒 OP。
    【解决方案3】:

    你可以用这个:

     @Around("@annotation(dumpFile)")
      public Object logExecutionTime(ProceedingJoinPoint joinPoint,DumpToFile dumpFile) 
    

    @annotation 内必须是DumpToFile 参数名称。

    详情请见documentation

    【讨论】:

    • 实际上这个答案是最新的,但在我修复其他人的代码之前第一个是正确的。如果我是 OP,我会接受这个。
    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多