How to inject config properties in Spring Boot to Spring Retry annotation?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Hardcoding retry limits in a @Retryable annotation works for a demo, but it becomes awkward as soon as different environments need different values. Spring Retry supports property-based configuration directly, so you can keep retry behavior externalized in application.yml or application.properties without giving up the convenience of annotations.
Use Expression Attributes Instead of Fixed Values
The key detail is that @Retryable has expression-based attributes such as maxAttemptsExpression, and @Backoff has delayExpression. Those are the ones that can read property placeholders.
This is the simplest solution and usually the right one. The values stay in configuration, while the service keeps the retry policy close to the method that needs it.
Enable Retry Support
The annotation will not do anything until retry support is enabled. In a Spring Boot application that normally means adding @EnableRetry on a configuration class.
If you skip this step, the application starts normally, but no retries happen because the proxy that applies retry behavior is never created.
Use a Properties Bean for More Complex Policies
If several services share the same retry settings, bind the values once with @ConfigurationProperties and reference that bean from the annotation with SpEL. This keeps the configuration strongly typed and easier to test.
This form is useful when values need validation, reuse, or documentation beyond a single annotation.
Understand the Proxy Boundary
Spring Retry is proxy-based. That means the retry logic is applied when another bean calls the annotated method through the proxy. A call from one method to another method in the same class does not pass through the proxy, so retry will not trigger.
This matters more than the property injection itself, because many developers correctly configure maxAttemptsExpression and still see no retries due to self-invocation. If you need internal reuse, move the retried method to another bean or use a programmatic RetryTemplate.
Common Pitfalls
- Using
maxAttemptsinstead ofmaxAttemptsExpression, which hardcodes the value and ignores configuration. - Forgetting
@EnableRetry, so the annotations are present but never applied. - Calling the annotated method from the same class, which bypasses the Spring proxy and skips retry behavior.
- Typing the property key incorrectly, which causes placeholder resolution to fail at startup or use the wrong value.
- Expecting runtime config changes to update the annotation immediately, even though the proxy usually reads values during bean creation.
Summary
- Put retry numbers in configuration and reference them with expression attributes.
- Use
maxAttemptsExpressionanddelayExpressionrather than fixed annotation values. - Enable retry support with
@EnableRetry. - Bind shared settings with
@ConfigurationPropertieswhen the policy is reused. - Remember that Spring Retry depends on proxy-based method invocation, not direct self-calls.
Related reading
- How to inject different services at runtime based on a property with Spring without XML
- How to inject environmental variables inside spring xml configuration?
- How to install a specific JDK on Mac OS X?
- How to install Java 8 on Mac
- How to install JDK 11 under Ubuntu?
- How to install Maven 3 on Ubuntu 18.04/17.04/16.10/16.04 LTS/15.10/15.04/14.10/14.04 LTS/13.10/13.04 by using apt-get?
- How to install spring boot CLI on Mac?
- How to install the JDK on Ubuntu Linux

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.