gpt4 book ai didi

java - 如何在没有注释的情况下在 Spring 核心中初始化 MongoClient 时注入(inject) Mongo 选项?

转载 作者:行者123 更新时间:2023-11-30 06:11:33 27 4
gpt4 key购买 nike

我想使用副本集名称初始化 MongoDB 集群连接池,以便万一我的主节点发生故障并且集群中的其他机器成为主节点,那么它应该可以工作,如下所示:

MongoClientOptions options = MongoClientOptions.builder()
.requiredReplicaSetName("ReplicaSetName").
build();

ServerAddress serverAddress1 = new ServerAddress("192.168.5.5");
ServerAddress serverAddress2 = new ServerAddress("192.168.5.6"); List<ServerAddress> seeds = new ArrayList<>();
seeds.add(serverAddress1);seeds.add(serverAddress2);
MongoClient mongoClient = new MongoClient(seeds,options);

上面的代码工作得很好,但我想在 Spring context.xml 中做同样的事情。

目前我的应用程序上下文如下所示:

    <bean id="mongoServerAddr1" class="com.mongodb.ServerAddress">
<constructor-arg name="host" value="${MONGO_CLUSTER1_HOST1}" />
<constructor-arg name="port" value="${MONGO_CLUSTER1_PORT1}" />
</bean>
<bean id="mongoServerAddr2" class="com.mongodb.ServerAddress">
<constructor-arg name="host" value="${MONGO_CLUSTER1_HOST2}" />
<constructor-arg name="port" value="${MONGO_CLUSTER1_PORT2}" />
</bean>


<util:list id="mongoHostList" value-type="com.mongodb.ServerAddress">
<ref bean="mongoServerAddr1" />
<ref bean="mongoServerAddr2" />
</util:list>


<bean id="mongoCredentialIDCluster1" class="com.mongodb.MongoCredential" factory-method="createScramSha1Credential">
<constructor-arg type="java.lang.String" name="userName" value="${MONGO_CLUSTER1_USER1}" />
<constructor-arg type="java.lang.String" name="source" value="${MONGO_CLUSTER1_SOURCE1}" />
<constructor-arg type="char[]" name="password" value="${MONGO_CLUSTER1_PASS1}" />
</bean>

<bean id="mongoCredentialIDCluster2" class="com.mongodb.MongoCredential" factory-method="createScramSha1Credential">
<constructor-arg type="java.lang.String" name="userName" value="${MONGO_CLUSTER1_USER2}" />
<constructor-arg type="java.lang.String" name="source" value="${MONGO_CLUSTER1_SOURCE2}" />
<constructor-arg type="char[]" name="password" value="${MONGO_CLUSTER1_PASS2}" />
</bean>

<util:list id="mongoCredentialList" value-type="com.mongodb.MongoCredential">
<ref bean="mongoCredentialIDCluster1" />
<ref bean="mongoCredentialIDCluster2" />
</util:list>

<bean id="mongoClient" class="com.mongodb.MongoClient">
<constructor-arg name="seeds" ref="mongoHostList" />
<constructor-arg name="credentialsList" ref="mongoCredentialList" />
</bean>
<bean id="mongoDao" class="com.dao.MongoDaoImpl">
<constructor-arg ref="mongoClient" />
</bean>

我怎样才能实现这个目标?

最佳答案

我已经用 MongoClientURI 类解决了这个问题。配置详细信息:

MONGO_URI=mongodb://user:password@192.168.50.167:27017,192.168.50.169:27017/?authSource=admin&replicaSet=ReplicaSetName

这是我的应用程序上下文配置:

    <bean id="mongoURI" class="com.mongodb.MongoClientURI">
<constructor-arg name="uri" value="${MONGO_URI}" />
</bean>

<bean id="mongoCLIENT" class="com.mongodb.MongoClient">
<constructor-arg ref="mongoURI" />
</bean>
<bean id="mongoDao" class="com.dao.MongoDaoImpl">
<constructor-arg ref="mongoCLIENT" />
</bean>

关于java - 如何在没有注释的情况下在 Spring 核心中初始化 MongoClient 时注入(inject) Mongo 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50137755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com