MongoDB整合Spring
MongoDB和Spring整合,根据开发文档找到对应的版本
Spring Data Document 1.x binaries requires JDK level 6.0 and above, and Spring Framework 3.0.x and above.
In terms of document stores, MongoDB preferably version 1.6.5 or later or CouchDB 1.0.1 or later are required.
根据官方文档知道Spring必须是3.0.x或以上的版本,MongoDB必须是1.6.5以上的版本才行。
要想整合Spring和Mongodb必须下载相应的jar,这里主要是用到两种jar一种是spring-data-document和spring-data-commons两种类型的jar但是两种类型的jar版本必须匹配以免引起jar的冲突因为commons后面的版本jar目录有所改变所有版本必须要对应好下面是jar下载地址
http://www.springsource.org/spring-data/mongodb
http://www.springsource.org/spring-data/commons
下面是我下载的jar还有就是添加mongodb java驱动包,spring jar添加我就不多说了
Spring配置文件如下:
添加实体类:
根据你的地址,我下载的jar中,mongoTemplate对应的class=“org.springframework.data.mongodb.core.MongoTemplate”
可是项目启动时总是异常,如下: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
我猜测是因为MongoTemplate无默认构造方法造成的,有没有解决方案,谢谢 2 楼 b30164 2012-06-18 知道了 是jar包版本的问题 真是烦人