You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot构造器注入List<String>因Sleuth多同类型Bean启动失败如何解决

错误原因

你的代码通过构造方法注入List<String>类型参数时,触发了Spring的集合注入逻辑:当注入的参数是List<T>类型时,Spring会默认把容器中所有类型为T的Bean全部收集后注入到该集合中。
你这里的T为String,而项目引入的Sleuth自动配置类恰好注册了4个String类型的Bean,就是报错信息里列出的4个链路配置相关的Bean,Spring无法识别你要注入的是自定义的tableIds配置值,因此抛出找到多个匹配Bean的错误。

解决办法

根据你的使用场景可以任选以下方案:

  • 方案1:用@Value明确指定绑定的配置项,直接加在构造参数上即可:
    @Configuration
    public class MyclassName{
    
        private final List<String> tableIds;
    
        // 替换${your.config.table-ids}为你实际配置的key
        public MyclassName(@Value("${your.config.table-ids:}") List<String> tableIds) {
            this.tableIds = tableIds;
        }
    }
    
  • 方案2:改用@ConfigurationProperties做配置绑定,避免和容器内其他String类型Bean冲突:
    @Configuration
    // 替换your.config为你实际的配置前缀
    @ConfigurationProperties(prefix = "your.config")
    public class MyclassName{
    
        private List<String> tableIds;
    
        public List<String> getTableIds() {
            return tableIds;
        }
    
        public void setTableIds(List<String> tableIds) {
            this.tableIds = tableIds;
        }
    }
    
  • 方案3:如果你的需求是注入Sleuth提供的某一个指定配置Bean,直接用@Qualifier指定Bean名称即可,示例:
    public MyclassName(@Qualifier("spring.sleuth.baggage-keys") List<String> tableIds) {
        this.tableIds = tableIds;
    }
    

内容的提问来源于stack exchange,提问作者Sweety

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 18:15:08