hip*_*uff 1 grails spring mongodb morphia mlab
我有一个使用 MongoLab 作为数据库的 Grails 2.4.4 应用程序。我以前使用 GORM 的 mongo 插件连接到数据库(请参阅此处的插件)。这不是一个好的体验,所以我正在切换到 Morpia 并从 GORM 切换到 mongodb。
我目前有 morphia 库版本 1.0.1 作为项目中的编译时依赖项。我在 Mongolab 中创建了一个用户,用户名:用户名,密码:pass(不是实际的凭证......)
我正在尝试使用 MongoClient 连接到数据库。我一直在查看文档(此处的文档)并且我也参考了这个问题,但它使用的是旧的连接方式。
这是我的错误,即使我没有看到抛出此错误的原因,因为我的字符串格式似乎是正确的:
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
Run Code Online (Sandbox Code Playgroud)
这是我在 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'
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏,谢谢。
您应该为 MongoClient 提供一个 MongoClientURI
MongoClientURI mongoClientURI = new MongoClientURI("connection string");
MongoClient mongoClient = new MongoClient(mongoClientURI);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4180 次 |
| 最近记录: |