2017-03-31 138 views
1

在spring引导应用程序中,我在yaml文件中定义了一些配置属性,如下所示。如何将Spring Boot中的配置属性注入到Spring Retry注释中?

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春重试标注值是多少?在下面的示例中,我想将maxAttempts的值10和退避值的500L替换为config属性的相应引用。

@Retryable(maxAttempts=10, include=TimeoutException.class, [email protected](value = 500L)) 

回答

6

spring-retry-1.2.0盯着我们可以@Retryable注释使用配置属性。

使用“maxAttemptsExpression”,请参考下面的代码使用,

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

,如果你使用任何版本少比1.2.0.Also你不需要任何配置属性班,它不会起作用。

1

您可以使用Spring EL,如下图所示加载性能:

@Retryable(maxAttempts="${my.app.maxAttempts}", 
    include=TimeoutException.class, 
    [email protected](value ="${my.app.backOffDelay}")) 
1

您还可以在表达式属性中使用现有的bean。

@Retryable(include = RuntimeException.class, 
      maxAttemptsExpression = "#{@retryProperties.getMaxAttempts()}", 
      backoff = @Backoff(delayExpression = "#{@retryProperties.getBackOffInitialInterval()}", 
           maxDelayExpression = "#{@retryProperties.getBackOffMaxInterval" + "()}", 
           multiplierExpression = "#{@retryProperties.getBackOffIntervalMultiplier()}")) 
    String perform(); 

    @Recover 
    String recover(RuntimeException exception); 

其中

retryProperties

是你的bean持有重试相关属性,如你的情况。

相关问题