问题描述
我似乎无法获得使用spring-mongodb初始化的最基本的MongoTemplate.
I can't seem to get the most basic MongoTemplate to initialize with spring-mongodb.
以下是我的POM的相关摘录:
Here's the relevant extract from my POM:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
我的XML上下文设置如下:
My XML context setup looks like this:
<mongo:db-factory id="mongoDbFactory"
host="${mongo.host}"
port="${mongo.port}"
dbname="${mongo.db}"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
结果是:
java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper
org.springframework.data.mongodb.core.convert.MappingMongoConverter.<init>(MappingMongoConverter.java:104)
org.springframework.data.mongodb.core.MongoTemplate.getDefaultMongoConverter(MongoTemplate.java:1670)
org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:205)
org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:191)
...
调试DefaultMongoTypeMapper
的初始化显示:
java.lang.NoSuchMethodError: org.springframework.data.util.ClassTypeInformation.from(Ljava/lang/Class;)Lorg/springframework/data/util/TypeInformation;
我为spring-core和spring-data尝试了各种不同的版本组合,并尝试手动指定spring-data-commons的版本,没有骰子.
I've tried all sorts of different version combinations for spring-core and spring-data, as well as trying to manually specify a version of spring-data-commons, no dice.
在这里我做错了什么基本的事情吗?
Is there something basic I'm doing wrong here?
完整的错误跟踪是:
Error creating bean with name 'mongoTemplate' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.data.util.ClassTypeInformation.from(Ljava/lang/Class;)Lorg/springframework/data/util/TypeInformation;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
推荐答案
如您提交的票据所示,Spring Data MongoDB 1.5.2需要Spring Data Commons 1.8.2(或任何其他1.8.x版本). Spring版本必须至少为3.2.10(尽管Spring本身不会出现异常,因为它与Spring Data Common的ClassTypeInformation
中的微小签名更改有关).如果是这样,您将看不到原始异常.
As indicated in the ticket you filed, Spring Data MongoDB 1.5.2 requires Spring Data Commons 1.8.2 (or any other 1.8.x version). The Spring version has to be at least 3.2.10 (although Spring itself doesn't play into the exception you see as it is about a tiny signature change in Spring Data Common's ClassTypeInformation
). If that's the case, you won't see the original exception.
我们通常建议使用 Spring Data Releasetrain BOM 或 Spring IO平台,而不是手动处理版本.
We generally recommend to use the Spring Data Releasetrain BOM or the Spring IO platform instead of manually dealing with versions.
这篇关于为什么Spring Data MongoDB 1.5.2会因NoSuchMethodError而失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!