Search code examples
javalambdaspring-beancircuit-breaker

Where is the parameter in this spring bean comming from


I am new to Spring and need to use the circuit breaker pattern, so I looked at the Spring Cloud Circuit Breaker project and saw this code

@Bean
public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() {
    return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
        .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build())
        .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
        .build());
 }

where is the factory coming from? is it injected?

The project where this code came from is here demo

cheers,

es


Solution

  • There is nothing injected.

    The methods return a new instance of Customizer<Resilience4JCircuitBreakerFactory>

    The code could also be written like this:

    @Bean
    public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() {
            return new Customizer<Resilience4JCircuitBreakerFactory>() {
                @Override
                public void customize(Resilience4JCircuitBreakerFactory factory) {
                    factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
                            .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(3)).build())
                            .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
                            .build());
                }
            };
        }
    

    But because Customizer is a functional interface it can be written in a lambda.