升级到spring boot 2.6.7导致循环依赖
Upgrading to springboot 2.6.7 leads to cyclic dependency
我正在将我的项目从 springboot 2.4.4 升级到 2.6.7,但是在较新的版本中,由于循环依赖,我遇到了这个运行时错误:
***************************
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]
└─────┘
这是我的 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);
}
});
}
}
我不确定为什么这种循环依赖会发生在新版本而不是旧版本上。谁能帮我解决一下?
Spring 引导会自动将每个 MeterFilter
bean 添加到 auto-configured MeterRegistry
,因此不需要当前形式的 MeterRegistryConfig
。我将用定义 MeterFilter
bean 的 @Configuration
class 替换它。像这样:
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);
}
};
}
}
我正在将我的项目从 springboot 2.4.4 升级到 2.6.7,但是在较新的版本中,由于循环依赖,我遇到了这个运行时错误:
***************************
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]
└─────┘
这是我的 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);
}
});
}
}
我不确定为什么这种循环依赖会发生在新版本而不是旧版本上。谁能帮我解决一下?
Spring 引导会自动将每个 MeterFilter
bean 添加到 auto-configured MeterRegistry
,因此不需要当前形式的 MeterRegistryConfig
。我将用定义 MeterFilter
bean 的 @Configuration
class 替换它。像这样:
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);
}
};
}
}