Why is Spring Data MongoDB 1.5.2 not working with NoSuchMethodError?

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

Here is a relevant excerpt 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 leads to:

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 initialization DefaultMongoTypeMapper

shows:

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

      

I've tried all sorts of combinations of spring-core and spring-data versions and also tried to manually specify the spring-data-commons version, no bones.

Is there something basic that I am doing wrong here?

EDIT: Complete error:

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)

      

+3


source to share


4 answers


As stated in the ticket you submitted, Spring Data MongoDB 1.5.2 requires Spring Data Commons 1.8.2 (or any other 1.8.x version). The Spring version must be at least 3.2.10 (although Spring itself does not play in the exception you see as it is a tiny signature change in Spring Data Common ClassTypeInformation

). If this happens, you will not see the original exception.



We generally recommend using the Spring DataLetesetrain specification or Spring IO framework instead of manual versioning.

+3


source


Try to make your dependencies this way;). You may 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/

+1


source


OK .. so it seems like there is an incompatibility somewhere. It is unclear, but the stack trace indicates that it is AutowiredAnnotationBeanPostProcessor

trying to call a method ClassTypeInformation

that does not exist. This method was last seen in 1.7.3-RELEASE spring-data-commons. Reverting to this version seems to fix the situation, but I'm not sure if it will lead to errors elsewhere.

The last 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>

      

+1


source


Try to include spring-data-commons Jar. This class is defined here. I guess that's why this is causing the problem.

0


source







All Articles