如何在Spring Boot中将配置属性注入到Spring Retry注释中?
在Spring Boot应用程序中,我在yaml文件中定义了一些配置属性,如下所示。
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;
}
如何注入Spring
Retry注释的值my.app.maxAttempts
和my.app.backOffdelay
?在下面的示例中,我想用配置属性的相应引用替换10
maxAttempts和500L
backoff值。
@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的任何版本,它将不起作用。此外,您不需要任何可配置的属性类。
-
如何将属性值注入使用注释配置的 Spring Bean?
2022-05-26 关注 0 浏览14 1答案
-
如何在Spring中将依赖项注入到自实例对象中?
2021-02-02 关注 0 浏览121 1答案
-
Java 如何在Spring中将依赖项注入到自实例对象中?
2021-02-02 关注 0 浏览97 1答案
-
如何在Spring Boot中将GenericFilterBean数据传递到WebSecurityConfigurerAdapter?
2021-02-02 关注 0 浏览84 1答案
-
如何在Spring Boot中覆盖Spring Security默认配置
2021-02-02 关注 0 浏览76 1答案
-
Spring Boot外部化Java注释上的配置属性/消息
2021-02-02 关注 0 浏览86 1答案
-
如何在Spring Boot 2.1.0启动程序配置中将spring.main.allow-bean-definition- overriding设置为true
2021-02-02 关注 0 浏览514 1答案
-
如何在Spring Boot + Spring Security应用程序中配置CORS?
2021-02-02 关注 0 浏览591 1答案
-
如何在spring-boot中禁用spring-data-mongodb自动配置
2021-02-02 关注 0 浏览147 1答案
-
如何在Spring Boot中全局配置@DateTimeFormat模式?
2021-02-02 关注 0 浏览105 1答案