Pregunta

Getting following error while connecting to mongodb using Spring Data:

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:228)
at com.mongodb.DBPort.go(DBPort.java:112)
at com.mongodb.DBPort.call(DBPort.java:79)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:218)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305)
at com.mongodb.DB.getCollectionNames(DB.java:284)
at org.springframework.data.mongodb.core.MongoTemplate$14.doInDB(

Please find my configuration :

@Configuration
public class AppConfig {

public @Bean MongoOperations mongoTemplate(Mongo mongo) {
    MongoTemplate mongoTemplate = new MongoTemplate(mongo, "mydb");
    return mongoTemplate;
}

/*
 * Factory bean that creates the Mongo instance
 */
public @Bean MongoFactoryBean mongo() {
    MongoFactoryBean mongo = new MongoFactoryBean();
    mongo.setHost("10.6.120.180");
    mongo.setPort(27017);
    return mongo;
}

/*
 * Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes
 */
public @Bean PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
    return new PersistenceExceptionTranslationPostProcessor();
}

}

Mongo-context-test.xml

    <mongo:db-factory dbname="mydb"/>
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongoDbFactory"/>
</bean>

I am new to Spring Data and MongoDB. Please advice, why am I getting this error?

Thanks in advance.

¿Fue útil?

Solución

Are you sure that your AppConfig class is being used?

As mentioned in my comment the error you are receiving indicates that your program is attempting to connect to MongoDB on server 127.0.0.1 while the AppConfig class you have posted clearly shows that you want to connect to IP address 10.6.120.180.

This can happen if you are somehow picking up an AppConfig class that contains mongo.setHost("localhost") which is in the example code posted on Github.

Update: Based on OP's comment the class reference for mongoTemplate in Mongo-context-test.xml was pointing to the Spring Framework's default MongoTemplate class.

According to the SpringSource documentation Introduction to MongoTemplate use can use Java to create and register an instance of MongoTemplate OR configure a MongoTemplate using Spring's XML <beans/> schema.

Obviously using the <beans/> schema takes precedence over your implementation in Java. If you use the <beans/> schema then you need to use <mongo:mongo host="localhost" port="27017"/> to specify your host and port requirements as noted by the OP in his comment.

Otros consejos

If you plan to define your own configuration beans, with your custom names, you can exclude Spring's auto-configuration classes by doing:

@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

OR

@SpringBootApplication(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

Remember: when using EnableAutoConfiguration/SpringBootApplication with exclude list make sure there are no other classes annotated with EnableAutoConfiguration or SpringBootApplication.

Source: How to disable spring-data-mongodb autoconfiguration in spring-boot

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top