无法使用Grails 3.1.1连接到mongo DB

时间:2016-03-07 21:01:32

标签: grails gorm gorm-mongodb

我从头开始创建应用程序,当我启动应用程序时,出现以下错误。我的MongoDB版本是2.6.1

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: source can not be null
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 44 common frames omitted
Caused by: java.lang.IllegalArgumentException: source can not be null
    at com.mongodb.assertions.Assertions.notNull(Assertions.java:37) ~[mongodb-driver-core-3.2.1.jar:na]
    at com.mongodb.MongoCredential.<init>(MongoCredential.java:233) ~[mongodb-driver-core-3.2.1.jar:na]
    at com.mongodb.MongoCredential.createCredential(MongoCredential.java:106) ~[mongodb-driver-core-3.2.1.jar:na]
    at org.grails.datastore.gorm.mongo.bean.factory.MongoClientFactoryBean.afterPropertiesSet(MongoClientFactoryBean.java:145) ~[grails-datastore-gorm-mongodb-5.0.1.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]

我的DataSource配置如下

grails {
            mongodb {
                host = "localhost"
                port = 27017
                username = "admin"
                password = "password"
                databaseName = "sta"
            }
        }

3 个答案:

答案 0 :(得分:0)

检查official guide

  

如果你想配置Grails如何连接到MongoDB,你可以使用grails-app / conf / application.yml中的以下设置来实现:

grails:
    mongodb:
        host: "localhost"
        port: 27017
        username: "blah"
        password: "blah"
        databaseName: "foo"

另请参阅下一章: 2.3高级配置

答案 1 :(得分:0)

我尝试了.yml文件和.groovy文件。我还创建了一个新的样本,但它没有用。问题是随3.1.1版一起提供的插件。一旦我升级到3.1.3,问题就消失了。

答案 2 :(得分:0)

我认为GORM插件中存在一个错误。它似乎无法识别databaseName属性。如果您使用连接字符串,它似乎确实有效。

connectionString:&#34; mongodb://用户名:密码@ host:port / databaseName&#34;

详情: https://docs.mongodb.com/manual/reference/connection-string/