[英]How to inject config properties in Spring Boot to Spring Retry annotation?
In spring boot application, I define some config properties in yaml file as below. 在spring启动应用程序中,我在yaml文件中定义了一些配置属性,如下所示。
my.app.maxAttempts = 10
my.app.backOffDelay = 500L
And an example bean 还有一个例子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;
}
How can I inject the values of my.app.maxAttempts
and my.app.backOffdelay
to Spring Retry annotation? 如何将
my.app.maxAttempts
和my.app.backOffdelay
的值注入Spring Retry注释? In the example below, I want to replace the value 10
of maxAttempts and 500L
of backoff value with the corresponding references of config properties. 在下面的示例中,我想将maxAttempts的值
10
和500L
的退避值替换为配置属性的相应引用。
@Retryable(maxAttempts=10, include=TimeoutException.class, backoff=@Backoff(value = 500L))
Staring from spring-retry-1.2.0 we can use configurable properties in @Retryable annotation. 从spring-retry-1.2.0开始,我们可以在@Retryable注释中使用可配置属性。
Use "maxAttemptsExpression", Refer the below code for usage, 使用“maxAttemptsExpression”,请参阅以下代码以了解用法,
@Retryable(maxAttemptsExpression = "#{${my.app.maxAttempts}}",
backoff = @Backoff(delayExpression = "#{${my.app. backOffDelay}}"))
It will not work if you use any version less than 1.2.0.Also you don't require any configurable property classes. 如果使用任何小于1.2.0的版本,它将无法工作。此外,您不需要任何可配置的属性类。
You can also use existing beans in expression attributes. 您还可以在表达式属性中使用现有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);
where 哪里
retryProperties
retryProperties
is your bean which holds retry related properties as in your case. 是你的bean,它保存与你的情况相关的重试相关属性。
You can use Spring EL as shown below to load the properties: 您可以使用如下所示的Spring EL来加载属性:
@Retryable(maxAttempts="${my.app.maxAttempts}",
include=TimeoutException.class,
backoff=@Backoff(value ="${my.app.backOffDelay}"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.