PK0513
PK0513

Reputation: 57

java 8 Springboot How to use properties from application.yml file in annotations?


I am using java 8, Spring boot 2.2 and have a piece of code where I use `@Retryable`. The backoff, maxattempt fields as of now are hardcoded and I wish to make them configurable. But only constants are allowed in there; have to be final. Any idea on how to overcome this ? I remember seeing some code somewhere which makes me think its possible. I know I can do it via `RetryTemplate`. But I cant digest the idea that there is no other way. I want this.

@Retryable( value = { SQLException.class },maxAttempts = 2,backoff =
@Backoff(delay = 5000))

I to become something like this

@Retryable( value = { SQLException.class },maxAttempts =
"${applicationyml['myproperty']}",backoff = @Backoff(delay =
"${applicationyml['myproperty']}"))

Upvotes: 0

Views: 1430

Answers (1)

Gary Russell
Gary Russell

Reputation: 174719

Use maxAttemptsExpression etc;

They can get property placeholders ${some.property} where some.property is in the YAML, or SpEL expressions

#{@someBean.someProperty}

Example here.

@Retryable(exceptionExpression = "#{@exceptionChecker.${retryMethod}(#root)}",
        maxAttemptsExpression = "#{@integerFiveBean}", backoff = @Backoff(delayExpression = "#{${one}}",
                maxDelayExpression = "#{${five}}", multiplierExpression = "#{${onePointOne}}"))
public void service3() {
    ...
}

Upvotes: 2

Related Questions