Spring Data Mongo seems to ignore host in XML configuration

烂漫一生 提交于 2020-01-11 06:20:17

问题


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.

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>

The program:

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);
    }
}

The exception thrown:

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)

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.

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.


回答1:


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

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" />

This should work too:

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


来源:https://stackoverflow.com/questions/16744260/spring-data-mongo-seems-to-ignore-host-in-xml-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!