在gorm/groovy上使用mongodb启动时的springboot初始化错误

raf*_*ian 2 groovy spring grails-orm mongodb spring-boot

使用MongoDB/GORM/Groovy on Java 1.7.0_55,gradle 1.11spring-boot-gradle-plugin:1.2.1.RELEASE.获取此springboot项目的启动问题.

我正在按原样运行项目,application.yml我的远程mongodb 的以下更改除外:

spring:
    mongodb:
        host: "10.160.8.1"
        databaseName: "citydb"
Run Code Online (Sandbox Code Playgroud)

**在启动时,我看到这个bean初始化错误 mappingMongoConverter

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]:
Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.data.mongodb.core.convert.MongoConverter]: : Error creating bean with name 'mappingMongoConverter' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]:
Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.data.mongodb.core.mapping.MongoMappingContext]: : No qualifying bean of type [org.springframework.data.mongodb.core.mapping.MongoMapping
Context] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.core.mapping.MongoMappingCo
ntext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mappingMongoConverter' defined in class path resource [org
/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 1 of typ
e [org.springframework.data.mongodb.core.mapping.MongoMappingContext]: : No qualifying bean of type [org.springframework.data.mongodb.core.mapping.MongoMappin
gContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested excepti
on is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.core.mapping.MongoMappingC
ontext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Run Code Online (Sandbox Code Playgroud)

我认为这是主要问题:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mappingMongoConverter' defined in class path resou
rce [org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index
1 of type [org.springframework.data.mongodb.core.mapping.MongoMappingContext]: : No qualifying bean of type [org.springframework.data.mongodb.core.mapping.Mon
goMappingContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested
 exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.core.mapping.Mongo
MappingContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:
1111)
Run Code Online (Sandbox Code Playgroud)

这可能是GORM依赖问题吗?我正在使用最新的,org.grails:gorm-mongodb-spring-boot:1.1.0.RELEASE

在github上跟踪问题:https://github.com/spring-guides/gs-accessing-data-gorm-mongodb/issues/6

And*_*son 7

gorm-mongodb-spring-boot与Spring Boot 1.2不兼容.它创建了一个名为bean的bean mongoMappingContext,它可以防止MongoDataAutoConfiguration创建具有相同名称的bean.你应该在日志中看到这样的消息:

2015-02-25 14:01:30.731  INFO 60231 --- [           main] a.ConfigurationClassBeanDefinitionReader : Skipping bean definition for [BeanMethod:name=mongoMappingContext,declaringClass=org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration]: a definition for bean 'mongoMappingContext' already exists. This top-level bean definition is considered as an override.
Run Code Online (Sandbox Code Playgroud)

您可以通过org.springframework.data.mongodb.core.mapping.MongoMappingContext使用不同的名称声明自己的bean 来解决此问题,例如:

@Bean
MongoMappingContext springDataMongoMappingContext() {
    return new MongoMappingContext()
}
Run Code Online (Sandbox Code Playgroud)

  • 这并不是真正值得遵循的事情。这是针对“gorm-mongodb-spring-boot”长达 6 年之久的问题的解决方法,目前有望得到解决。 (2认同)