Brixton.M4 + zuul:预期单个匹配 bean 但发现 2:dropwizardMetricServices、servoMetricServices
Brixton.M4 + zuul: Expected single matching bean but found 2: dropwizardMetricServices,servoMetricServices
我正在尝试将 Spring Boot + Spring 基于云的项目升级到 Brixton.M4,因为 zuul 在打包的版本中不支持 PATCH (https://github.com/spring-cloud/spring-cloud-netflix/issues/412) SC 的 Brixton.M3。
我启用了 spring-boot-starter-actuator 和 spring-cloud-starter-zuul,但现在容器无法启动并出现以下错误:
No qualifying bean of type [org.springframework.boot.actuate.metrics.CounterService] is defined:
expected single matching bean but found 2: dropwizardMetricServices,servoMetricServices
更多堆栈跟踪:
Could not autowire field: private org.springframework.boot.actuate.metrics.CounterService org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration.counterService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [org.springframework.boot.actuate.metrics.CounterService] is defined: expected single matching bean but found 2: dropwizardMetricServices,servoMetricServices
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 34 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.boot.actuate.metrics.CounterService] is defined: expected single matching bean but found 2: dropwizardMetricServices,servoMetricServices
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 36 more
类路径中确实有两个 CounterService 类型的 bean:打包在 spring-boot-actuator-1.3.1.RELEASE.jar 中的 DropwizardMetricServices 和位于 spring-cloud-netflix 中的 ServoMetricServices -core-1.1.0.M4.jar
有什么方法可以禁用其中之一吗?我检查了文档,但找不到任何明显的方法。
谢谢!
您可以在 java 配置文件中使用 @Primary
,如下所示:
@Bean
@Primary
public DropwizardMetricServices dropwizardMetricServices(){
return new DropwizardMetricServices();
}
对上述答案的小修正
@Bean
@Primary
public DropwizardMetricServices dropwizardMetricServices(MetricRegistry metricRegistry){
return new DropwizardMetricServices(metricRegistry);
}
我正在尝试将 Spring Boot + Spring 基于云的项目升级到 Brixton.M4,因为 zuul 在打包的版本中不支持 PATCH (https://github.com/spring-cloud/spring-cloud-netflix/issues/412) SC 的 Brixton.M3。 我启用了 spring-boot-starter-actuator 和 spring-cloud-starter-zuul,但现在容器无法启动并出现以下错误:
No qualifying bean of type [org.springframework.boot.actuate.metrics.CounterService] is defined:
expected single matching bean but found 2: dropwizardMetricServices,servoMetricServices
更多堆栈跟踪:
Could not autowire field: private org.springframework.boot.actuate.metrics.CounterService org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration.counterService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [org.springframework.boot.actuate.metrics.CounterService] is defined: expected single matching bean but found 2: dropwizardMetricServices,servoMetricServices
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 34 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.boot.actuate.metrics.CounterService] is defined: expected single matching bean but found 2: dropwizardMetricServices,servoMetricServices
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 36 more
类路径中确实有两个 CounterService 类型的 bean:打包在 spring-boot-actuator-1.3.1.RELEASE.jar 中的 DropwizardMetricServices 和位于 spring-cloud-netflix 中的 ServoMetricServices -core-1.1.0.M4.jar
有什么方法可以禁用其中之一吗?我检查了文档,但找不到任何明显的方法。
谢谢!
您可以在 java 配置文件中使用 @Primary
,如下所示:
@Bean
@Primary
public DropwizardMetricServices dropwizardMetricServices(){
return new DropwizardMetricServices();
}
对上述答案的小修正
@Bean
@Primary
public DropwizardMetricServices dropwizardMetricServices(MetricRegistry metricRegistry){
return new DropwizardMetricServices(metricRegistry);
}