Spring Boot Sping Boot 构造函数中出现意外错误

syqv5f0l  于 2023-03-02  发布在  Spring
关注(0)|答案(3)|浏览(222)

我写了一个示例程序如下。

  1. @Configuration
  2. public class MyclassName{
  3. private final List<String> tableIds;
  4. public MyclassName(
  5. List<String> tableIds) {
  6. this.tableIds = tableIds;
  7. }
  8. }

运行时,我得到下面的错误。

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. Parameter 0 of constructor in MyclassName required a single bean, but 4 were found:
  6. - spring.sleuth.baggage-keys: defined by method 'baggageKeys' in class path resource [org/springframework/cloud/sleuth/autoconfig/TraceBaggageConfiguration.class]
  7. - spring.sleuth.local-keys: defined by method 'localKeys' in class path resource [org/springframework/cloud/sleuth/autoconfig/TraceBaggageConfiguration.class]
  8. - spring.sleuth.propagation-keys: defined by method 'propagationKeys' in class path resource [org/springframework/cloud/sleuth/autoconfig/TraceBaggageConfiguration.class]
  9. - spring.sleuth.log.slf4j.whitelisted-mdc-keys: defined by method 'whiteListedMDCKeys' in class path resource [org/springframework/cloud/sleuth/autoconfig/TraceBaggageConfiguration.class]
  10. Action:
  11. Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
  12. Process finished with exit code 0

我们使用sleuth进行跟踪,所以pom.xml也有依赖关系。但这是一个非常基本的例子,为什么我会得到这个错误。有人能帮助吗?

j91ykkif

j91ykkif1#

MyclassName对于spring框架是已知的。
所以spring会尝试使用构造函数来创建MyclassName的一个示例:

  1. public MyclassName(List<String> tableIds)

Parameter 0 of constructor in MyclassNameList<String> tableIds
Spring试图为这个参数提供一个值,但是它找到了4,而不是只有一个可用的值。所以Spring不够聪明,不应该选择一个代替你。
4个可用值由Sleuth提供,并在TraceBaggageConfiguration.class中声明,如下所示:

  1. @Bean(BAGGAGE_KEYS)
  2. @ConfigurationProperties(BAGGAGE_KEYS)
  3. List<String> baggageKeys() {...}
  4. @Bean(LOCAL_KEYS)
  5. @ConfigurationProperties(LOCAL_KEYS)
  6. List<String> localKeys() {...}
  7. @Bean(PROPAGATION_KEYS)
  8. @ConfigurationProperties(PROPAGATION_KEYS)
  9. List<String> propagationKeys() {...}
  10. @Bean(WHITELISTED_MDC_KEYS)
  11. @ConfigurationProperties(WHITELISTED_MDC_KEYS)
  12. List<String> whiteListedMDCKeys() {...}

为了解决这种犹豫不决的问题,您需要通过使用唯一标识符来告诉Spring您真正想要在构造函数中注入哪个List<String>
首先限定(给予一个id)预期的bean

  1. @Bean("myExpectedTableIds")
  2. List<String> myTableIdsProcucerMethod()

然后使用org.springframework.beans.factory.annotation.Qualifier注解告诉Spring您真正想要的bean:

  1. public MyclassName(@Qualifier("myExpectedTableIds") List<String> tableIds)
展开查看全部
baubqpgj

baubqpgj2#

  1. public MyclassName(
  2. List<String> tableIds) {
  3. this.tableIds = tableIds;
  4. }

这意味着你的MyclassNames,作为Spring中的一个Configuration bean,需要注入一个类型为List<String>的bean,碰巧sluethTraceBaggageConfiguration类定义了一些类型为List<String>的bean:

  1. @org.springframework.context.annotation.Bean({"spring.sleuth.baggage-keys"})
  2. @org.springframework.boot.context.properties.ConfigurationProperties("spring.sleuth.baggage-keys")
  3. java.util.List<java.lang.String> baggageKeys() { /* compiled code */ }
  4. @org.springframework.context.annotation.Bean({"spring.sleuth.local-keys"})
  5. @org.springframework.boot.context.properties.ConfigurationProperties("spring.sleuth.local-keys")
  6. java.util.List<java.lang.String> localKeys() { /* compiled code */ }
  7. @org.springframework.context.annotation.Bean({"spring.sleuth.propagation-keys"})
  8. @org.springframework.boot.context.properties.ConfigurationProperties("spring.sleuth.propagation-keys")
  9. java.util.List<java.lang.String> propagationKeys() { /* compiled code */ }

因此spring报告了一个错误,不知道要注入哪个bean。您可能需要使用Qualifier来指定要注入的bean。或者,您是否定义了对应的bean,该bean散列了需要注入的List<String>类型?MyclassName中的List<String> tableIds是什么?您需要如下定义Bean

  1. public MyclassNamesss(
  2. @Qualifier("test") List<String> tableIds) {
  3. this.tableIds = tableIds;
  4. }
  5. @Bean("test")
  6. public List<String> myString() {
  7. return new ArrayList<>();
  8. }

或者,您可以使用,在本例中,tableId将为null

  1. public MyclassName(
  2. @Autowired(required = false) List<String> tableIds) {
  3. this.tableIds = tableIds;
  4. }
展开查看全部
5ssjco0h

5ssjco0h3#

不管怎样,因为我发现这篇文章在寻找相同的错误消息,所以我在Sping Boot 中使用Lombok的@RequiredArgsConstructor注入List<String>

  1. @Component
  2. @RequiredArgsConstructor
  3. @ConfigurationProperties(prefix = "some-prefix")
  4. public static class SomeProperties {
  5. @Getter
  6. private final List<String> patterns;
  7. }

并通过手动初始化列表修复了该问题:

  1. @Component
  2. @ConfigurationProperties(prefix = "some-prefix")
  3. public static class SomeProperties {
  4. @Getter
  5. private final List<String> patterns = new ArrayList<>();
  6. }

然后用Spring属性正确地填充该列表。

展开查看全部

相关问题