Upgrading to springboot 2.6.7 leads to cyclic dependency

3.2k views Asked by At

I am upgrading my prooject from springboot 2.4.4 to 2.6.7 but in the newer version I am getting this runtime error due to cyclic dependency:

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   webMvcMetricsFilter defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.class]
┌─────┐
|  prometheusMeterRegistry defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]
↑     ↓
|  meterRegistryConfig defined in URL [jar:file:/Users/myname/Documents/repo/myrepo/my-module/bin/my-module.jar!/com/mypackage/app/metrics/MeterRegistryConfig.class]
└─────┘

This is my MeterRegistryConfig class:

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MeterRegistryConfig implements MeterFilter {

  @Autowired
  public MeterRegistryConfig(final MeterRegistry registry) {
    registry
        .config()
        .meterFilter(
            new MeterFilter() {
              @Override
              public DistributionStatisticConfig configure(
                  @NotNull final Meter.Id id, @NotNull final DistributionStatisticConfig config) {
                return DistributionStatisticConfig.builder()
                    .percentilesHistogram(true)
                    .percentilePrecision(3)
                    .build()
                    .merge(config);
              }
            });
  }
}

I am not sure why this cyclic dependency is happening with the newer version and not the older one. Can anyone please help me resolving it?

1

There are 1 answers

0
Andy Wilkinson On BEST ANSWER

Spring Boot will automatically add every MeterFilter bean to the auto-configured MeterRegistry so your MeterRegistryConfig in its current form isn't needed. I'd replace it with a @Configuration class that defines a MeterFilter bean. Something like this:

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import org.jetbrains.annotations.NotNull;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
class MeterRegistryConfig {

    @Bean
    MeterFilter yourMeterFilter() {
        return new MeterFilter() {
            @Override
            public DistributionStatisticConfig configure(
                    @NotNull final Meter.Id id,
                    @NotNull final DistributionStatisticConfig config) {
                return DistributionStatisticConfig.builder()
                    .percentilesHistogram(true)
                    .percentilePrecision(3)
                    .build()
                    .merge(config);
            }
        };
    }

}