spring-data-mongodb報錯

今天在使用spring-data-mongodb進行mongo操做的時候遇到下面這個錯誤java

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [spring.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.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStdspring

at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)mongodb

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049)數據庫

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)spring-mvc

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)mvc

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)socket

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)測試

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)spa

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292).net

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.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:106)

at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:57)

at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)

at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)

at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)

at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)

... 27 more


這是spring.xml文件的配置信息

<?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:mongo="http://www.springframework.org/schema/data/mongo"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">


       <mongo:mongo id="mongo" replica-set="192.168.47.129:27017">

           <mongo:options

                   connections-per-host="8"

                   threads-allowed-to-block-for-connection-multiplier="3"

                   connect-timeout="10000"

                   max-wait-time="20000"

                   auto-connect-retry="true"

                   socket-keep-alive="true"

                   socket-timeout="1500"

                   write-number="1"

                   write-timeout="0"

                   write-fsync="true"/>

       </mongo:mongo>


       <!--mongo的鏈接工廠,dbname爲鏈接的數據庫名,沒有則會自動建立一個,mongo-ref爲關聯的mongo實例 -->

       <mongo:db-factory dbname="users" mongo-ref="mongo"/>


       <!-- mongodb的主要操做對象,全部對mongodb的增刪改查的操做都是經過它完成 -->

       <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

              <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>

       </bean>


</beans>


測試類

package com.dreyer.springMongodb.customer;


import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import java.util.List;



/**

 * @author Dreyer

 * @description

 * @email hy.dreyer@qq.com

 * @date 2016/1/13 15:10

 */

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:spring.xml","classpath:spring-mvc.xml" })

public class MainTest {

    @Autowired

    private MongoTemplate mongoTemplate;


    @Test

    public void test() {

        List<Customer> list = mongoTemplate.findAll(Customer.class);

        for (Customer customer : list) {

            System.out.println(customer.getFirstName());

        }

    }

}


spring-data-mongodb的版本爲:1.8.2,spring的版本爲:3.2.0

報錯緣由是由於spring-data-mongodb1.6以上的版本須要配置spring4.0以上

解決方案就是把spring版本改成4.1.6便可

相關文章
相關標籤/搜索