当前位置: 首页 > 知识库问答 >
问题:

Spring data mongo db版本与Spring的兼容性

李昌勋
2023-03-14

这是我的pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.0.0.RELEASE</version>      
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.5.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

这是我的Bean配置

<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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
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
      http://www.springframework.org/schema/util 
      http://www.springframework.org/schema/util/spring-util-4.1.xsd
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task-4.0.xsd">

<mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}"/> 
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" username="${mongo.username}" password="${mongo.password}" mongo-ref="mongo"/>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
    <constructor-arg ref="mongoDbFactory"/> 
</bean>

<bean id="awayLogDao" class="com.zymr.mongo.repository.AwayLogDao">
    <constructor-arg ref="mongoTemplate"/>
</bean>

当我尝试运行我的项目时,我会遇到以下异常

SEVERE: Exception sending context initialized event to listener instance of class 
org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionParser for element [mongo]
Offending resource: class path resource [conf/spring/ApplicationContext.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.fatal(FailFastProblemReporter.java:62)
at org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:90)
at org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:68)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.findParserForElement(NamespaceHandlerSupport.java:86)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1366)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1352)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:178)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:148)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:98)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:507)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:223)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:194)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:620)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:521)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)



java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:176)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1035)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1011)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:963)
at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:525)
at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我尝试更改spring和spring数据mongodb的版本。我使用了spring版本-3.2.2。发布而不是5.0.0。发布和spring数据mongodb版本-1.2.0。释放使用旧版本后,一切正常。但我想使用最新版本的SpringDataMongoDB,因为它支持聚合操作。

我想使用spring data mongodb执行以下查询:

db.mycollection.aggregate([
    { "$redact": { 
        "$cond": [ 
            { "$and": [ 
                { "$eq": [ { "$year": "$date" }, 2017 ] }, 
                { "$eq": [ { "$month": "$date" }, 10 ] }
            ] }, 
            "$$KEEP", 
            "$$PRUNE" 
        ] } 
    }
]) 

我该如何实现呢?哪个版本的Spring我需要使用最新的Spring数据mongoDB?

共有3个答案

淳于兴朝
2023-03-14

我最近更新了spring和MongoDB,它运行良好。我正在使用以下依赖项:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.7.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>2.1.1.RELEASE</version>
     </dependency>  
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.4.2</version>
    </dependency>

这是我的数据库连接。xml。升级后需要在mongoTemplate中添加mongoConverter。

<?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-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.10.2.xsd">
<mongo:mongo-client id="mongo" host="${db.host}" port="${db.port}">

<mongo:client-options connect-timeout="${db.connectTimeout}"
        connections-per-host="${db.connectionPerHost}"
        threads-allowed-to-block-for-connection-multiplier="${db.threadsAllowedTblockForConnectionMultiplier}"/>
</mongo:mongo-client>

<mongo:db-factory dbname="${db.name}" host="${db.host}" mongo-ref="mongo" port="${db.port}" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoConverter" ref="mappingConverter" />
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<bean id="mongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
    <constructor-arg name="typeKey"><null/></constructor-arg>
</bean>

<bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />

<bean id="mappingConverter"     class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mappingContext" ref="mongoMappingContext" />
    <property name="typeMapper" ref="mongoTypeMapper"></property>
</bean>
</beans>
全彬
2023-03-14

您可以尝试运行下面的命令,通过mvn检查您选择的SpringCore版本的所有依赖项

mvn dependency:tree

另一种方法是在pom中的依赖树的最后定义Spring数据mongob依赖

姜锋
2023-03-14

只是在尝试更新Spring Boot项目中的依赖项时遇到了同样的问题。长话短说后更新版本的org.springframework.data: sping-data-mongob从1到2我不得不更新应用程序上下文文件中的XML命名空间以及http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd-

 类似资料:
  • 我正在尝试构建一个微服务应用程序,它将使用Hystrix和一些来自Spring云的分布式跟踪。当我尝试将Spring Cloud版本“2021.0.0-M3”用于Spring boot版本“2.5.5”时,启动时出现以下错误: ***由于以下原因,您的项目设置与我们的要求不兼容: Spring Boot[2.5.5]与此Spring Cloud发布训练不兼容*** Spring Boots和Spr

  • 我正在使用生成cacerts。 然后我使用,但我会遇到这样的例外: 这是兼容性问题吗?使用一个版本的java生成的Cacert是否与另一个版本的java不兼容?

  • 我们目前正在使用以下框架及其版本。 Spring启动父-1.4.5。发布 Vaadin-Bom-7.7.3 Vaadin-Spring启动-1.1.0 Vaadin-Spring-1.1.0 计划升级Spring引导父版本。但我敢肯定,如果我们升级Spring引导父版本,那么需要升级Vaadin依赖。我花了几天时间找到最新的Vaadin框架与Spring Boot jar的兼容性。但无法得出解决方

  • 我在pom上使用下面的代码。xml,但由于出现错误,我无法启动应用程序。我不想降级spring boot starter父版本。有什么解决办法吗? 我得到了贝沃的错误 组织。springframework。豆。工厂BeanCreationException:创建名为“configurationPropertiesBeans”的bean时出错,该bean在类路径资源[org/springframew

  • 问题内容: 我需要很快将应用程序升级到Java 11,我想知道与Java 11兼容的最低Spring版本是多少。 我目前正在使用Java 8和Spring 4.2.7。 问题答案: 从他们的任务跟踪器中 -SPR-16391:与JDK 11的兼容性 。计划与 5.1 GA 版本一起实现JDK 10、11的兼容性。 JDK 11当前计划于2018年9月发布。从我们的角度来看,除了JDK 10之外,

  • 我目前使用的是Java8和Spring4.2.7。