0
votes

It seems i just can't make this mongoTemplate bean to work in my project

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/data/mongo
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<mongo:mongo id="mongo" replica-set="localhost:27017"></mongo:mongo>
<mongo:db-factory dbname="test" mongo-ref="mongo" />

<bean id="natureRepository" class="com.orangeslate.naturestore.repository.NatureRepositoryImpl">
    <property name="mongoTemplate" ref="mongoTemplate" />
</bean>

<!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>

<!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
<context:annotation-config />

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'natureRepository' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [applicationContext.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.IllegalAccessError: tried to access method org.springframework.core.GenericTypeResolver.getTypeVariableMap(Ljava/lang/Class;)Ljava/util/Map; from class org.springframework.data.util.ClassTypeInformation
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at com.orangeslate.naturestore.test.MongoTest.main(MongoTest.java:14)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [applicationContext.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.IllegalAccessError: tried to access method org.springframework.core.GenericTypeResolver.getTypeVariableMap(Ljava/lang/Class;)Ljava/util/Map; from class org.springframework.data.util.ClassTypeInformation
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 15 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.IllegalAccessError: tried to access method org.springframework.core.GenericTypeResolver.getTypeVariableMap(Ljava/lang/Class;)Ljava/util/Map; from class org.springframework.data.util.ClassTypeInformation
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:108)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
    ... 24 more
Caused by: java.lang.IllegalAccessError: tried to access method org.springframework.core.GenericTypeResolver.getTypeVariableMap(Ljava/lang/Class;)Ljava/util/Map; from class org.springframework.data.util.ClassTypeInformation
    at org.springframework.data.util.ClassTypeInformation.<init>(ClassTypeInformation.java:96)
    at org.springframework.data.util.ClassTypeInformation.<clinit>(ClassTypeInformation.java:42)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.<clinit>(MappingMongoConverter.java:79)
    at org.springframework.data.mongodb.core.MongoTemplate.getDefaultMongoConverter(MongoTemplate.java:1335)
    at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:185)
    at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:172)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
    ... 26 more


Anyone knows if the minimum requirement of the mongoDB spring data support is for spring-core to be 3.0.7??

  1. here are my some jars:(i use myeclipse10 to import Spring 3.0 Core Libraries)
    • org.springframework.core-3.0.5.RELEASE.
    • com.springsource.org.apache.commons.logging-1.1.1.jar
    • com.springsource.org.apache.log4j-1.2.15.jar
    • org.springframework.asm-3.0.5.RELEASE.jar
    • org.springframework.beans-3.0.5.RELEASE.jar
    • org.springframework.context-3.0.5.RELEASE.jar +org.springframework.expression-3.0.5.RELEASE.jar
    • spring-data-mongodb-1.0.0.M4.jar.
    • spring-data-mongodb-log4j-1.0.0.M4.jar.
    • spring-data-commons-core-1.2.0.RELEASE.jar
    • spring-data-mongodb-cross-store-1.0.0.M4.jar
1
The Spring version used by Spring data Mongo seems to be 3.0.6. Can you post the complete stack trace? - Andrei Stefan
Also, are you sure you are not mixing some Spring versions in there? - Andrei Stefan
Why do you use a milestone instead of a final release?! - M. Deinum
@AndreiStefan i have posted all the exception. - HFX
sorry,i'm freshman,i'm just want to make it available.can you tell me more detail ablout it? - HFX

1 Answers

0
votes

I strongly suggest to use a dependency management tool like maven or gradle saves you a lot of time searching for the right dependencies. Next I would also suggest moving to a RELEASE version of spring-data-mongodb instead of an old milestone. The current version is 1.5.0.RELEASE.

A pom.xml should make it work (and remove all the other dependencies you manually imported).

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>foo</groupId>
    <artifactId>mongodb-sample</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <spring-data-mongodb.version>1.5.0.RELEASE</spring-data-mongodb.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>${spring-data-mongodb.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb-log4j</artifactId>
            <version>${spring-data-mongodb.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb-cross-store</artifactId>
            <version>${spring-data-mongodb.version}</version>
        </dependency>
    </dependencies>


</project>

This will load the most recent version and due to maven it will include the transitive dependencies. No need to search for other dependencies. If you need more just include them in the <dependencies> section.

The pom will load the following dependencies:

[INFO] ------------------------------------------------------------------------
[INFO] Building mongodb-sample 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mongodb-sample ---
[INFO] foo:mongodb-sample:jar:1.0.0-SNAPSHOT
[INFO] +- org.springframework.data:spring-data-mongodb:jar:1.5.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-tx:jar:3.2.9.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.2.9.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-aop:jar:3.2.9.RELEASE:compile
[INFO] |  |     \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.2.9.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:3.2.9.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:3.2.9.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.8.0.RELEASE:compile
[INFO] |  +- org.mongodb:mongo-java-driver:jar:2.12.1:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.7.7:runtime
[INFO] +- org.springframework.data:spring-data-mongodb-log4j:jar:1.5.0.RELEASE:compile
[INFO] |  \- log4j:log4j:jar:1.2.16:compile
[INFO] \- org.springframework.data:spring-data-mongodb-cross-store:jar:1.5.0.RELEASE:compile
[INFO]    +- org.springframework:spring-aspects:jar:3.2.9.RELEASE:compile
[INFO]    |  \- org.aspectj:aspectjweaver:jar:1.7.4:compile
[INFO]    +- org.springframework:spring-orm:jar:3.2.9.RELEASE:compile
[INFO]    |  \- org.springframework:spring-jdbc:jar:3.2.9.RELEASE:compile
[INFO]    \- org.aspectj:aspectjrt:jar:1.8.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------