加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Spring Boot中将配置属性注入Spring Retry注释?

发布时间:2020-12-15 01:23:24 所属栏目:大数据 来源:网络整理
导读:在spring启动应用程序中,我在yaml文件中定义了一些配置属性,如下所示. my.app.maxAttempts = 10my.app.backOffDelay = 500L 还有一个例子bean @ConfigurationProperties(prefix = "my.app")public class ConfigProperties { private int maxAttempts; privat

在spring启动应用程序中,我在yaml文件中定义了一些配置属性,如下所示.

my.app.maxAttempts = 10
my.app.backOffDelay = 500L

还有一个例子bean

@ConfigurationProperties(prefix = "my.app")
public class ConfigProperties {
  private int maxAttempts;
  private long backOffDelay;

  public int getMaxAttempts() {
    return maxAttempts;
  }

  public void setMaxAttempts(int maxAttempts) {
    this.maxAttempts = maxAttempts;
  }

  public void setBackOffDelay(long backOffDelay) {
    this.backOffDelay = backOffDelay;
  }

  public long getBackOffDelay() {
    return backOffDelay;
  }

如何将my.app.maxAttempts和my.app.backOffdelay的值注入Spring Retry注释?在下面的示例中,我想将maxAttempts的值10和500Lof的退避值替换为配置属性的相应引用.

@Retryable(maxAttempts=10,include=TimeoutException.class,backoff=@Backoff(value = 500L))
最佳答案
从spring-retry-1.2.0开始,我们可以在@Retryable注释中使用可配置属性.

使用“maxAttemptsExpression”,请参阅以下代码以了解用法,

 @Retryable(maxAttemptsExpression = "#{${my.app.maxAttempts}}",backoff = @Backoff(delayExpression = "#{${my.app. backOffDelay}}"))

如果使用任何小于1.2.0的版本,它将无法工作.此外,您不需要任何可配置的属性类.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读