使用 Spring Data 连接到 mongodb 时出现以下错误:
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(
请找到我的配置:
@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>
我是 Spring Data 和 MongoDB 的新手。请指教,为什么我会收到这个错误?
提前致谢。