Why does Spring Data MongoDB 1.5.2 fail a with NoSuchMethodError?

一曲冷凌霜 提交于 2019-12-13 14:33:23

问题


I can't seem to get the most basic MongoTemplate to initialize with spring-mongodb.

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>

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>

This results in:

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)
...

Debugging the initialization of DefaultMongoTypeMapper reveals:

java.lang.NoSuchMethodError: org.springframework.data.util.ClassTypeInformation.from(Ljava/lang/Class;)Lorg/springframework/data/util/TypeInformation;

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?

EDIT: Full trace of error is:

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)

回答1:


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.

We generally recommend to use the Spring Data Releasetrain BOM or the Spring IO platform instead of manually dealing with versions.




回答2:


Try making your dependencies like this ;). You might be missing something from the tutorial page.

<dependencies>

    <!-- Spring framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>

    <!-- mongodb java driver -->
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>2.11.0</version>
    </dependency>

    <!-- Spring data mongodb -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>

</dependencies>

source: http://www.mkyong.com/mongodb/spring-data-mongodb-hello-world-example/




回答3:


OK.. so it seems there is an incompatibility somewhere. It's unclear but the stack trace seems to indicate that AutowiredAnnotationBeanPostProcessor is trying to call a method of ClassTypeInformation that doesn't exist. This method was last seen in 1.7.3-RELEASE of spring-data-commons. Reverting to that version appears to fix things, but I'm not sure if this will cause errors elsewhere.

The final POM looks like this:

<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-commons</artifactId>
    <version>1.7.3.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>



回答4:


Try including spring-data-commons Jar. This class is defined here. I guess this is why it is causing the issue.



来源:https://stackoverflow.com/questions/25234964/why-does-spring-data-mongodb-1-5-2-fail-a-with-nosuchmethoderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!