<legend id='CYljl'><style id='CYljl'><dir id='CYljl'><q id='CYljl'></q></dir></style></legend>
  • <small id='CYljl'></small><noframes id='CYljl'>

    <i id='CYljl'><tr id='CYljl'><dt id='CYljl'><q id='CYljl'><span id='CYljl'><b id='CYljl'><form id='CYljl'><ins id='CYljl'></ins><ul id='CYljl'></ul><sub id='CYljl'></sub></form><legend id='CYljl'></legend><bdo id='CYljl'><pre id='CYljl'><center id='CYljl'></center></pre></bdo></b><th id='CYljl'></th></span></q></dt></tr></i><div id='CYljl'><tfoot id='CYljl'></tfoot><dl id='CYljl'><fieldset id='CYljl'></fieldset></dl></div>

          <bdo id='CYljl'></bdo><ul id='CYljl'></ul>

      1. <tfoot id='CYljl'></tfoot>

        Spring Data Mongo 似乎忽略了 XML 配置中的主机

        时间:2024-08-23
      2. <i id='Mo5cl'><tr id='Mo5cl'><dt id='Mo5cl'><q id='Mo5cl'><span id='Mo5cl'><b id='Mo5cl'><form id='Mo5cl'><ins id='Mo5cl'></ins><ul id='Mo5cl'></ul><sub id='Mo5cl'></sub></form><legend id='Mo5cl'></legend><bdo id='Mo5cl'><pre id='Mo5cl'><center id='Mo5cl'></center></pre></bdo></b><th id='Mo5cl'></th></span></q></dt></tr></i><div id='Mo5cl'><tfoot id='Mo5cl'></tfoot><dl id='Mo5cl'><fieldset id='Mo5cl'></fieldset></dl></div>

          • <bdo id='Mo5cl'></bdo><ul id='Mo5cl'></ul>

          • <tfoot id='Mo5cl'></tfoot>

            <small id='Mo5cl'></small><noframes id='Mo5cl'>

          • <legend id='Mo5cl'><style id='Mo5cl'><dir id='Mo5cl'><q id='Mo5cl'></q></dir></style></legend>

                  <tbody id='Mo5cl'></tbody>

                  本文介绍了Spring Data Mongo 似乎忽略了 XML 配置中的主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Spring-Data 和 MongoDB 启动并运行一个简单的Hello World"程序.Spring 似乎忽略了在 <mongo:mongo/> 元素中配置的 MongoDB 主机 IP 地址,而是尝试连接到 127.0.0.1.

                  I am trying to get a simple "Hello World" program with Spring-Data and MongoDB up and running. Spring seems to be ignoring the MongoDB host IP address configured in the <mongo:mongo/> element and trying to connect to 127.0.0.1 instead.

                  根据各种教程,这是我的 Spring 配置 XML:

                  As per various tutorials, here is my Spring Configuration XML:

                  <beans xmlns="http://www.springframework.org/schema/beans"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                      xmlns:context="http://www.springframework.org/schema/context"
                      xmlns:mongo="http://www.springframework.org/schema/data/mongo"
                      xsi:schemaLocation="http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd
                            http://www.springframework.org/schema/data/mongo
                            http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
                            http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
                  
                      <mongo:mongo host="10.125.0.68" port="27017" />
                      <mongo:db-factory dbname="test" />
                  
                      <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
                          <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
                      </bean>
                  
                  </beans>
                  

                  程序:

                  import org.springframework.context.ApplicationContext;
                  import org.springframework.context.support.GenericXmlApplicationContext;
                  import org.springframework.data.mongodb.core.MongoOperations;
                  
                  public class Test1
                  {
                  
                      public static void main(String[] args) 
                      {
                          ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
                          MongoOperations mo = (MongoOperations)ctx.getBean("mongoTemplate");
                          DbDocument a = new DbDocument("John Smith", "jsmith@plymouth.org");
                          if (!mo.collectionExists(DbDocument.class))  //<<<----- Exception here
                              mo.createCollection(DbDocument.class);
                          mo.save(a);
                      }
                  }
                  

                  抛出的异常:

                  WARNING: Exception executing isMaster command on /127.0.0.1:27017
                  java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect
                      at com.mongodb.DBPort._open(DBPort.java:214)
                      at com.mongodb.DBPort.go(DBPort.java:107)
                      at com.mongodb.DBPort.go(DBPort.java:88)
                      at com.mongodb.DBPort.findOne(DBPort.java:143)
                      at com.mongodb.DBPort.runCommand(DBPort.java:148)
                      at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
                      at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527)
                      at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:277)
                      at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
                      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
                      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
                      at com.mongodb.DB.getCollectionNames(DB.java:412)
                      at com.mongodb.DB.collectionExists(DB.java:454)
                      at org.springframework.data.mongodb.core.MongoTemplate$5.doInDB(MongoTemplate.java:438)
                      at org.springframework.data.mongodb.core.MongoTemplate$5.doInDB(MongoTemplate.java:436)
                      at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:372)
                      at org.springframework.data.mongodb.core.MongoTemplate.collectionExists(MongoTemplate.java:436)
                      at org.springframework.data.mongodb.core.MongoTemplate.collectionExists(MongoTemplate.java:432)
                      at org.nwea.jhg.mongo.Test1.main(Test1.java:18)
                  

                  我可以使用 GUI 工具连接到指定地址 10.125.0.68 的 Mongo 实例,并确认有一个名为 test 的数据库.

                  I am able to connect to the Mongo instance at the specified address 10.125.0.68 with a GUI tool and confirm that there is a database named test.

                  我在 StackOverflow 中找到了几个匹配项,但没有一个与我的情况足够相似,无法用于解决问题.

                  I found a couple of hits in StackOverflow but neither one is sufficiently similar to my case to be of use in solving the problem.

                  推荐答案

                  您的 mongo db-factory 无法识别它应该使用哪个 MongoDB 数据库 IP.

                  Your mongo db-factory doesn't identify which MongoDB database IP it should use.

                  尝试使用引用 mongoTemplate 值的 db-factory :

                  Try to use a db-factory that references the mongoTemplate's value <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />:

                  <mongo:mongo id="mongo" host="10.125.0.68" port="27017" />
                  
                  <mongo:db-factory id="mongoDbFactory"
                                    host="10.125.0.68"
                                    port="27017"
                                    username="admin"
                                    password="xxx"
                                    dbname="test"
                                    mongo-ref="mongo" />
                  

                  这也应该有效:

                  <mongo:mongo id="mongo" host="10.125.0.68" port="27017" />
                  <mongo:db-factory dbname="test" mongo-ref="mongo" />
                  

                  这篇关于Spring Data Mongo 似乎忽略了 XML 配置中的主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <i id='9hePX'><tr id='9hePX'><dt id='9hePX'><q id='9hePX'><span id='9hePX'><b id='9hePX'><form id='9hePX'><ins id='9hePX'></ins><ul id='9hePX'></ul><sub id='9hePX'></sub></form><legend id='9hePX'></legend><bdo id='9hePX'><pre id='9hePX'><center id='9hePX'></center></pre></bdo></b><th id='9hePX'></th></span></q></dt></tr></i><div id='9hePX'><tfoot id='9hePX'></tfoot><dl id='9hePX'><fieldset id='9hePX'></fieldset></dl></div>

                      <tbody id='9hePX'></tbody>
                      <bdo id='9hePX'></bdo><ul id='9hePX'></ul>
                      <tfoot id='9hePX'></tfoot>
                    • <small id='9hePX'></small><noframes id='9hePX'>

                        1. <legend id='9hePX'><style id='9hePX'><dir id='9hePX'><q id='9hePX'></q></dir></style></legend>