Home > Enterprise >  Resilience4J Retry not auto-configured in Spring boot 3
Resilience4J Retry not auto-configured in Spring boot 3

Time:12-22

I'm in the process of migrating to Spring Boot 3. In Spring Boot 2 Resilience4J Retry was auto-configured and worked out of the box using the following setup:

application.yaml:

resilience4j.retry:
  instances:
    some-instance
      # retry config here

Test class:

@SpringBootTest
public class TestClass {

  @Autowired
  private RetryRegistry retryRegistry;

  @Test
  void someTest() {
    // perform test and evaluate retries using retryRegistry
  }
}

However while updating to Spring Boot 3 using the following versions:

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)

The test in which the RetryRegistry was autowired failed with the following message:

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)}

I managed to fix the test by explicitly importing the Resilience4j Retry configuration in the test using:

@Import(io.github.resilience4j.retry.autoconfigure.RetryAutoConfiguration.class)

However, I'm wondering why the component scanning mechanism in Spring Boot 3 did not pick up the retry config in the first place. Would anyone know why Spring Boot 3 did not pick up the class during component scanning?

CodePudding user response:

In the resilience4j project they changed the dependency for spring boot 3.

So you should go for io.github.resilience4j:resilience4j-spring-boot3:${resilience4jVersion}

like

org.springframework.boot:spring-boot-starter:jar:3.0.0:compile
io.github.resilience4j:resilience4j-spring-boot3:jar:2.0.0:compile 

from the documentation

CodePudding user response:

It seems that it is related to a new META-INF file being used instead of the old spring.factories file. From the documentation :

Spring Boot 2.7 introduced a new META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports file for registering auto-configurations, while maintaining backwards compatibility with registration in spring.factories. With this release, support for registering auto-configurations in spring.factories has been removed in favor of the imports file.

The Resilience4J dependency used in the spring-cloud-dependencies-parent BoM still uses a spring.factories file instead of the new file named org.springframework.boot.autoconfigure.AutoConfiguration.imports. The new file has recently been introduced in Resilience4J (source).

Overriding the version from the Spring BoM with version 2.0.2 for all the resilience dependencies fixed it for me. I will check in a few days whether the new Resilience4J version has been updated in the Spring BoM (or resilience4j-spring-boot3 has been introduced).

[edit] As others have noticed,resilience4j-spring-boot3 is already available. I'll start using it.

  • Related