如何将 Spring Boot 中的配置属性注入 Spring Retry 注释? [英] How to inject config properties in Spring Boot to Spring Retry annotation?
问题描述
在 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
和一个示例 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
和退避值的 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 500L
of 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.
这篇关于如何将 Spring Boot 中的配置属性注入 Spring Retry 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!