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

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

问题描述:

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

In spring boot application, I define some config properties in yaml file as below.

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

还有一个示例豆

@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.maxAttemptsmy.app.backOffdelay的值注入Spring Retry批注?在下面的示例中,我想用相应的配置属性引用替换maxAttempts的值10和退避值的500L.

How can I inject the values of my.app.maxAttempts and my.app.backOffdelay to Spring Retry annotation? In the example below, I want to replace the value 10 of maxAttempts and 500Lof backoff value with the corresponding references of config properties.

@Retryable(maxAttempts=10, include=TimeoutException.class, backoff=@Backoff(value = 500L))

spring-retry-凝视1.2.0 我们可以在@Retryable批注中使用可配置的属性.

Staring from spring-retry-1.2.0 we can use configurable properties in @Retryable annotation.

使用"maxAttemptsExpression",请参考以下代码以使用

Use "maxAttemptsExpression", Refer the below code for usage,

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

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

It will not work if you use any version less than 1.2.0.Also you don't require any configurable property classes.