@Qualifier 的问题

2024-01-10

我正在 Java Spring 环境中工作,并且在让 @Qualifier 工作时遇到问题。我们项目的其他部分正在使用 @Inject 来获取一个 bean,但我需要有同一个 bean 的两个版本,看起来使用 @Autowired 和 @Qualifier 应该可以解决问题,但我无法让它们工作。我可能错过了一件小事,但没有运气找到答案。

这是代码的相关部分。我一直在尝试各种事情,所以我可能有比我目前需要的更多的注释和参数。

public class MongoDbConfig {
    @Bean(name="sourceTemplate")
    @Qualifier("sourceTemplate")
    public MongoTemplate getSourceTemplate() {
        MongoTemplate mt = new MongoTemplate(getMongoDbFactory(sourceServers, sourceDatabaseName));
        return mt;
    }

    @Bean(name="destinationTemplate")
    @Qualifier("destinationTemplate")
    public MongoTemplate getDestinationTemplate() {
        MongoTemplate mt = new MongoTemplate(getMongoDbFactory(destinationServers, destinationDatabaseName));
        return mt;
    }
}

public class SourceDaoImpl implements SourceDao {
    @Autowired
    @Qualifier("sourceTemplate")
    private MongoOperations mongoOps;
}


public class DestinationDaoImpl implements DestinationDao {
    @Autowired
    @Qualifier("destinationTemplate")
    private MongoOperations mongoOps;
}

当我尝试启动我的应用程序时,我得到以下信息:

Parameter 1 of method gridFsTemplate in org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration required a single bean, but 2 were found:
    - sourceTemplate: defined by method 'getSourceTemplate' in MongoDbConfig
    - destinationTemplate: defined by method 'getDestinationTemplate' in MongoDbConfig

Action:

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

感谢您对我在这里缺少的内容提出任何建议。


尝试这个:

public class MongoDbConfig {
    @Bean(name = {"sourceTemplate", "mongoTemplate"})
    public MongoTemplate getSourceTemplate() {
        MongoTemplate mt = new MongoTemplate(getMongoDbFactory(sourceServers, sourceDatabaseName));
        return mt;
    }

    @Bean(name="destinationTemplate")
    public MongoTemplate getDestinationTemplate() {
        MongoTemplate mt = new MongoTemplate(getMongoDbFactory(destinationServers, destinationDatabaseName));
        return mt;
    }
}

public class SourceDaoImpl implements SourceDao {
    @Autowired
    @Qualifier("sourceTemplate")
    private MongoOperations mongoOps;
}


public class DestinationDaoImpl implements DestinationDao {
    @Autowired
    @Qualifier("destinationTemplate")
    private MongoOperations mongoOps;
}

Updated

其实方法:

@Bean
    @ConditionalOnMissingBean
    public GridFsTemplate gridFsTemplate(MongoDbFactory mongoDbFactory,
            MongoTemplate mongoTemplate) {
        return new GridFsTemplate(
                new GridFsMongoDbFactory(mongoDbFactory, this.properties),
                mongoTemplate.getConverter());
    }

in MongoDataAutoConfiguration需要名称为“mongoTemplate”的 MongoTemplate bean,但无法找到该 bean,而是您定义了自己的另外 2 个 bean“sourceTemplate”和“destinationTemplate”。

我想这会解决你的问题!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

@Qualifier 的问题 的相关文章

随机推荐

  • 使用 GDI+ 将修改后的图像保存到原始文件

    我正在从文件加载位图图像 当我尝试将图像保存到另一个文件时 出现以下错误 GDI 中发生一般错误 我相信这是因为文件被图像对象锁定 好的 所以尝试调用 Image Clone 函数 这仍然锁定文件 唔 接下来 我尝试从 FileStream
  • 删除目录下的所有文件

    我需要使用 Qt 删除目录中的所有文件 该目录中的所有文件都将具有扩展名 txt 我不想删除目录本身 有谁知道我该怎么做 我看过 QDir 但没有运气 比约恩斯的答案被调整为不会永远循环 QString path whatever QDir
  • 版本 4.0.0 文档中的示例中的 Identity Server 范围无效

    我正在使用 IdentityServer4 遵循以下文档https identityserver4 readthedocs io en latest quickstarts 1 client credentials html https i
  • OWL RDF/TTL 根据属性创建类的实例成员

    我正在尝试设计一个本体 该本体将根据产品组件对产品进行分类 在下面的例子中我有一堂课Ingredient有一个实例eggs 我想添加apple tart不包含的所有类别的产品eggs 所以在这种情况下apple tart将被添加到班级中Gl
  • 限制 UITextView 中的行数

    我很清楚有人问过这个问题 但我找不到有效的答案 使用先前解决方案的组合 我想出了以下代码 BOOL textView UITextView textView shouldChangeTextInRange NSRange range rep
  • Google App Engine 上的 CloudSQL PDO(unix_socket) 问题

    我正在尝试从 App Engine 连接到我的 CloudSQL 实例 我的设置方法是使用静态 IP 我可以使用它从 App Engine 外部 即在我的开发环境中 连接到它 但是 当应用程序在 GAE 中运行时 连接到数据库的唯一方法是使
  • 如何在 Matplotlib 中使用带有形状内文本的自定义标记?

    背景 在 Matplotlib 中 我们可以使用 mathtext 作为标记来渲染字符串 参考文献1 https matplotlib org 3 2 1 api markers api html Question 有什么方法可以将此文本封
  • UP3(jawbone)如何实时检索睡眠值

    我正在尝试实时检索我的睡眠数据 我无法从文档中找到如何实现它 UP平台Android SDK https github com Jawbone UPPlatform Android SDK 附 我愿意进行睡眠实验 需要在我到达时获取一个事件
  • Java EE / J2EE 与 J2SE / JDK 版本之间的关联

    我确信我会找到重复的问题或令人满意的答案 但我没有 我正在寻找的是这些问题的答案 Java EE J2EE 和 J2SE JDK 版本之间是否有官方关联 如果答案是肯定的 那么它写在哪里 如果答案是否定的 那为什么呢 是否有非官方版本表 例
  • Javascript原生排序方法代码

    知道如何查看本机 javascript 方法的实现 特别是排序 方法 我之所以寻找这个 我只是想知道所使用的算法是什么以及其复杂性是什么 我正在 javascript 中对一个巨大的 json 对象进行排序 我想知道我是否应该为此编写自己的
  • 有没有办法让 JSF 与通用 setter 和 getter 一起使用

    有没有办法让 JSF 与通用实体一起工作 如下所述 属性 java public interface MyProperty
  • 如何将我的所有函数打包在批处理文件中作为单独的文件?

    我的问题与this https stackoverflow com questions 10149194 something like a function method in batch files问题 我有几个需要从批处理文件执行的操作
  • 如何转换pandas中日期时间列的时区,然后删除时区?

    我有一个列 非索引列 其中包含日期时间 例如 前五个条目如下所示 Timestamp 2018 11 15 19 57 55 Timestamp 2018 11 15 19 59 46 Timestamp 2018 11 15 20 00
  • 如何将(声明)安全令牌传递给启用 WIF 的 WCF 服务

    我很想知道我们如何从已通过 WIF 启用的 WCF 服务身份验证的 WIF 应用程序发送安全令牌 任何帮助将不胜感激 答案并不简单 但以下步骤构成了 推荐模式 并记录在以下 MSDN 文章中 AD FS 2 0 身份委派分步指南 http
  • x86_64 调用约定和堆栈帧

    我试图理解 GCC 4 4 3 为 Ubuntu Linux 下运行的 x86 64 机器生成的可执行代码 特别是 我不明白代码如何跟踪堆栈帧 过去 在 32 位代码中 我习惯于在几乎每个函数中看到这个 序言 push ebp movl e
  • 从内部存储中删除文件

    我正在尝试删除存储在内部存储中的图像 到目前为止我已经想出了这个 File dir getFilesDir File file new File dir id jpg boolean deleted file delete 这是来自另一个问
  • 上限集合性能问题

    我正在做一些测试 看看我可以从 Mongodb 获得什么样的吞吐量 文档说上限集合是最快的选择 但我经常发现我可以更快地写入普通集合 根据具体的测试 我通常可以通过正常收集获得两倍的吞吐量 我错过了什么吗 我该如何解决这个问题 我有一个非常
  • 检索超过 7 天的特定用户的推文

    我正在尝试获取任何用户的推文 但它只返回最近 7 天的推文 我想检索比这更旧的推文 如何做 现在我正在通过以下方式获取推文 http search twitter com search atom q from 3Amihirpmehta h
  • bash:Python 导入 - 找不到 pandas 命令

    我是使用 MacBook 的 Python 初学者 我想进口pandas在我的 Python 脚本中 我输入以下命令 import pandas as pd 结果是 错误 bash 导入 找不到命令 问题 如何启用导入命令 我用了 usr
  • @Qualifier 的问题

    我正在 Java Spring 环境中工作 并且在让 Qualifier 工作时遇到问题 我们项目的其他部分正在使用 Inject 来获取一个 bean 但我需要有同一个 bean 的两个版本 看起来使用 Autowired 和 Quali