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

时间:2015-02-24 17:45:30

标签: spring mongodb groovy gorm spring-boot

Java 1.7.0_55,gradle 1.11spring-boot-gradle-plugin:1.2.1.RELEASE上获取此springboot project using MongoDB/GORM/Groovy的启动问题。

我正在运行项目 as-is ,但我对远程mongodb的application.yml进行了以下更改:

spring:
    mongodb:
        host: "10.160.8.1"
        databaseName: "citydb"

**启动时,我看到{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: {}

我认为这是主要问题:

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)

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

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

1 个答案:

答案 0 :(得分:7)

gorm-mongodb-spring-boot与Spring Boot 1.2不兼容。它创建了一个名为mongoMappingContext的bean,可以防止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.

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

@Bean
MongoMappingContext springDataMongoMappingContext() {
    return new MongoMappingContext()
}