【问题标题】:Application crashes when I use Mongo with Spring当我将 Mongo 与 Spring 一起使用时应用程序崩溃
【发布时间】:2016-04-02 08:36:35
【问题描述】:

我的应用程序在启动时崩溃并出现错误:

原因:org.xml.sax.SAXParseException;行号:13;列号:60; cvc-complex-type.2.4.c:匹配的通配符是严格的,但是找不到元素'mongo:mongo'的声明。

我的 context.xml 看起来像这样:

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.0.xsd">

 <mongo:mongo id="mongo" host="localhost" port="27017"/>

 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
 <constructor-arg ref="mongo"/>
 <constructor-arg name="databaseName" value="ProcessOrderManagement"/>
 </bean>

我的 Pom.xml 看起来像这样:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.8.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.2.0</version>
    </dependency>

    <dependency>
        <groupId>org.mongodb.morphia</groupId>
        <artifactId>morphia</artifactId>
        <version>1.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>1.5.0.RELEASE</version>
    </dependency>

请忽略两个 # 符号,因为我的代表不是 10,所以我无法粘贴链接,我输入了那个符号, 无论如何,我的应用程序在出现上述错误后崩溃。我做错了什么?

更新的 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-3.0.xsd
                http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
            ">


<mongo:mongo id="mongo" host="localhost" port="27017"/>


<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="ProcessOrderManagement"/>
</bean>

【问题讨论】:

  • /data/mongo/spring-mongo-1.0.xsd 而不是这个,只需定义 /data/mongo/spring-mongo.xsd 并重试
  • @BilboBaggins 试过了,同样的错误。抱歉没有提到我试过了。
  • 您需要正确发布您的context.xml。如果可能,完整文件,或至少前 30 行。一个字一个字。它在语法上不正确。

标签: java xml spring mongodb maven


【解决方案1】:

看起来你的 context.xml 要么粘贴错误,要么你的错误是你的 context.xml 没有正确打开。您缺少打开的 xml 文档标签。请注意此示例的顶部。

更新:如果没有的话,还要注意 spring 上下文命名空间的需要。

 <?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">

    <!-- Default bean name is 'mongo' -->
    <mongo:mongo host="localhost" port="27017"/>

</beans>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-10-06
  • 2020-01-23
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2022-08-03
  • 2020-06-08
  • 1970-01-01
相关资源
最近更新 更多