【问题标题】:SpringData and MongoDB configuration error : MongoTemplateSpringData 和 MongoDB 配置错误:MongoTemplate
【发布时间】:2016-11-20 14:30:55
【问题描述】:

我目前正在尝试将 mongodb 集成到我的 Spring 应用程序中。

我已经在我的机器上设置了初始的 mongodb 环境,服务启动等

但是在 spring 的应用程序配置中存在与 mongoTemplate construction-arg 相关的错误

“通配符是严格的,但没有找到元素的声明”

我实际上使用了与文档here 中描述的相同的 xml 支出,与最近的一些在线教程相同,据我所知,我已经添加了正确的命名空间 url。所以我现在有点迷失了我所缺少的东西。非常感谢您对此提供任何帮助。

这里看一下xml配置:

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />




    <!-- scanning comment root context of all components package directory-->
    <context:component-scan base-package="com.demo" />


    <!--Dispatcher servlet-->
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/view/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>


    <!-- Configure to plugin JSON as request and response in method handler -->
    <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter"/>
            </beans:list>
        </beans:property>
    </beans:bean>


    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean>


    <!-- mongo db config -->
    <mongo:mongo host="localhost" port="27017"  id="mongo" />

    <mongo:repositories base-package="com.demo.football.repository" />

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



</beans:beans>

【问题讨论】:

  • 你用的是什么版本的 spring-data 和 spring-data-mongodb ?
  • spring-mongo-mongodb 是 1.7.2 版,不需要 spring-data 依赖项-据我所知..

标签: spring mongodb spring-data persistence mongotemplate


【解决方案1】:

限定 xsd 名称以匹配您正在使用的 spring 版本。

考虑到您使用的是 Spring 4,xshema 命名空间将更改为以下。

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mongo="http://www.springframework.org/schema/data/mongo"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd">

【讨论】:

  • 这已经成功了。关于为什么会出现这种情况或春天很挑剔的任何解释?谢谢 Veeram!
  • 是的,consturctor-arg 的 name 属性仅在 spring 3 之后可用。因此,当您不限定 xsds 时,当 name 属性不可用时,spring 会将其解析为早期版本。跨度>
  • 非常好。在宏伟的计划中,我是春天的新手。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
相关资源
最近更新 更多