【问题标题】:Pass property to spring annotation from custom annotation从自定义注释将属性传递给弹簧注释
【发布时间】:2022-06-20 15:57:51
【问题描述】:

我在 spring 中有一个自定义注释,它利用了@Retryable 注释

@Target({ ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Retryable(value = MyClass.SubClassException.class,
    backoff = @Backoff(random = true,
        delayExpression = "#{${retry.delayExpression}}",
        maxDelayExpression = "#{${retry.maxDelay}}",
        multiplierExpression = "#{${retry.multiplier}}"),
    maxAttemptsExpression = "#{${retry.maxRetryAttempts}}")
public @interface CustomRetryable {

  int delayMilliSeconds() default 1000;

  int maxDelayMilliSeconds() default 1000;

  int multiplier() default 2;

  int retryAttempts() default 3;

  Class<? extends Throwable>[] exceptionType();
}

我想做的是能够用一个类和可能的readTimeout 和其他值调用我的注释,并将这些传递给@Retryable 注释。这可能吗?

我想做这样的事情:

@GlobalRetryable(exceptionType = MyCustom.SubClassException.class,
  delayMilliSeconds = 10)
Foo bar(Foozie request){
  // some impl heere
}

但我找不到将MyCustom.SubClassException.class 之类的东西传递给Retryable 注释的方法。

【问题讨论】:

    标签: spring-boot annotations


    【解决方案1】:

    我认为@GlobalRetryable@CustomRetryable 是一样的,只是一个错字。

    注释@AliasFor 可用于您的要求。这将是这样的:

    @Target({ ElementType.METHOD, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Retryable(value = MyClass.SubClassException.class,
        backoff = @Backoff(random = true,
            delayExpression = "#{${retry.delayExpression}}",
            maxDelayExpression = "#{${retry.maxDelay}}",
            multiplierExpression = "#{${retry.multiplier}}"),
        maxAttemptsExpression = "#{${retry.maxRetryAttempts}}")
    public @interface CustomRetryable {
      .....
      .....
      .....
      @AliasFor(annotation = Retryable.class, attribute = "value")
      Class<? extends Throwable>[] exceptionType();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多