spring - 尝试连接到MongoLab时URL格式的MongoException

我有一个使用MongoLab作为数据库的Grails 2.4.4应用程序。我以前是使用GORM的mongo插件(see plugin here)连接到数据库的。那还不算是一个很好的经验,所以我正在切换到Morpia并从GORM转到mongodb。

目前,我在项目中将morphia库版本1.0.1作为编译时的依赖项。
我已经在Mongolab中创建了一个用户名,用户名:username,密码:pass(不是真实的凭据...)

我正在尝试使用MongoClient连接到数据库。我一直在查看文档(documentation here),也引用了this question,但是它使用的是旧的连接方式。

这是我的错误,即使我看不到由于我的字符串格式似乎正确而引发此错误的原因:

ERROR context.GrailsContextLoaderListener
- Error initializing the application: Error creating bean with name 'datastore': 
Cannot resolve reference to bean 'mongoClient' while setting constructor argument; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'mongoClient': Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; 
nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
Message: Error creating bean with name 'datastore': Cannot resolve reference to bean 'mongoClient' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
Line | Method
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'mongoClient': 
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; 
nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanInstantiationException: Could not instantiate bean class [com.mongodb.MongoClient]: 
Constructor threw exception; nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by MongoException: host and port should be specified in host:port format
->>  122 | <init>    in com.mongodb.ServerAddress
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|     49 | <init>    in     ''
|    118 | <init> .  in com.mongodb.MongoClient
|    262 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . in java.lang.Thread
| Error Forked Grails VM exited with error

这是我在resources.groovy中拥有的代码片段,尝试将MongoClient,Morphia和我的数据存储注册为spring bean。
beans = {

String databaseUrl = "mongodb://username:pass@ds053251.mongolab.com:12345/trendapp" //fake port
String databaseName = "trendapp"

mongoClient(MongoClient, databaseUrl)
morphia(Morphia)

datastore(Datastore, ref('mongoClient'), databaseName) { bean ->
    bean.factoryBean = 'morphia'
    bean.factoryMethod = 'createDatastore'
}
}

任何帮助表示赞赏,谢谢。

最佳答案

您应该为MongoClient提供MongoClientURI

MongoClientURI mongoClientURI = new MongoClientURI("connection string");
MongoClient mongoClient = new MongoClient(mongoClientURI);

https://stackoverflow.com/questions/32322166/

相关文章:

grails - Groovy/Grails无法仅在运行时解析类

bash - 通过ssh登录时自动运行脚本

grails - 如何在 Grails 中对存储过程记录进行建模?

grails - 从过滤器访问 'session'

grails - Grails 3数据库迁移问题

php - 从php运行shell脚本

git - Git推送错误:ssh:无法解析主机名domain.com [:7555]:提供了节点名

svn - KDESvn 'Add ssh-identities to ssh-agent' 不起作

ubuntu - ssh 不会在 ubuntu 10.04 64bit 中自动启动

c# - 具有永久ShellExec连接的SharpSSH