Spring Boot 3中Resilience4J Retry未自动配置的原因排查
问题:Spring Boot 3中Resilience4J Retry自动配置失效的原因
我正在迁移至Spring Boot 3。在Spring Boot 2中,Resilience4J Retry可自动配置并开箱即用,配置如下:
application.yaml:
resilience4j.retry: instances: some-instance: # retry config here
测试类:
@SpringBootTest public class TestClass { @Autowired private RetryRegistry retryRegistry; @Test void someTest() { // perform test and evaluate retries using retryRegistry } }
升级到Spring Boot 3后,使用的依赖版本:
org.springframework.boot:spring-boot-starter:jar:3.0.0:compile io.github.resilience4j:resilience4j-spring-boot2:jar:1.7.0:compile (derived from a Spring BoM)
注入RetryRegistry的测试报错:
Unsatisfied dependency expressed through field 'retryRegistry': No qualifying bean of type 'io.github.resilience4j.retry.RetryRegistry' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我通过在测试中显式导入RetryAutoConfiguration解决了问题:
@Import(io.github.resilience4j.retry.autoconfigure.RetryAutoConfiguration.class)
想了解为何Spring Boot 3的组件扫描未自动识别该Retry配置?
原因分析
- 依赖版本不匹配:你使用的
resilience4j-spring-boot2是针对Spring Boot 2的适配包,Spring Boot 3需要使用专门适配的resilience4j-spring-boot3包。旧的spring-boot2适配包无法兼容Spring Boot 3的自动配置机制。 - 自动配置加载机制变更:Spring Boot 3废弃了Spring Boot 2中基于
spring.factories文件加载自动配置的方式,改用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件来声明自动配置类。resilience4j-spring-boot2仍使用旧的spring.factories文件,导致Spring Boot 3无法识别并加载Retry的自动配置类,因此不会生成RetryRegistryBean。 - 显式导入的临时作用:手动导入
RetryAutoConfiguration是直接强制Spring容器加载该配置类,从而生成所需Bean,但这只是临时 workaround。正确的解决方式是升级Resilience4J到适配Spring Boot 3的版本(至少1.8.0+,对应resilience4j-spring-boot3依赖),让自动配置机制正常工作。
内容的提问来源于stack exchange,提问作者Michiel
相关产品推荐
相关产品推荐

