Spring:Mongo配置找不到“mongo:repositories”

时间:2016-11-09 10:33:17

标签: java xml spring mongodb

我正在使用JEE项目,从Maven项目到Dynamic Web。 问题是我没有使用xml配置,但现在,我必须使用Dynamic web项目。

我无法弄清楚如何正确编写applicationContext.xml;我已经阅读了很多主题,但没有人帮助我。

这里有代码和错误:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:mongo="http://www.springframework.org/schema/data/mong‌​o"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd      
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.2.xsd
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
  http://www.springframework.org/schema/data/mongo
  http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

  <!-- Abilita l'uso di tutte le annotazioni -->
  <context:annotation-config/>
  <import resource="security-config.xml"/>

  <context:component-scan base-package="it.**"/>

  <!-- Abilita il supporto AOP -->
  <aop:aspectj-autoproxy proxy-target-class="true"/>
  <util:properties id="settings" location="classpath:../../META-INF/MANIFEST.MF"/>

  <!-- i valori delle properties vengono iniettati in AuthentificationFilter e il id=myProps serve per l'iniezione -->
  <util:properties id="myProps" location="classpath:muru/application.properties"/>

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="locations">
   <list>
     <value>classpath*:muru/*.properties</value>
   </list>
 </property>
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
 </bean>
 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg>
  <bean class="com.mongodb.MongoClient">
    <constructor-arg value="localhost"/>
    <constructor-arg value="27017"/>
  </bean>
  </constructor-arg>
  <constructor-arg value="database"/>
</bean>
 <mongo:repositories base-package="it.cap.domain" mongo-template-ref="mongoTemplate"></mongo:repositories>
    <!-- Abilita la configurazione delle transazioni tramite annotazioni -->
   <tx:annotation-driven transaction-manager="txManager"/>
   <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
   </beans>

堆栈跟踪

11:12:26.108 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 68 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 68; columnNumber: 93; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:repositories'.
at                      org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)

at java.lang.Thread.run(Thread.java:745)
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:repositories'.
你能帮我理解吗? 谢谢你的建议:)

2 个答案:

答案 0 :(得分:0)

问题似乎来自上下文文件中bean定义的顺序。

尝试更改以下bean定义的位置

 <tx:annotation-driven transaction-manager="txManager"/>

从好的位置到下面的豆之后。

<context:component-scan base-package="it.**"/>

这样的东西
<context:component-scan base-package="it.**"/>
<tx:annotation-driven transaction-manager="txManager"/>

看看这是否解决了您的问题。

供您参考.. http://forum.spring.io/forum/spring-projects/container/9895-order-of-bean-definitions-matters

如果以上解决方案不起作用,我想最后一件事就是检查在你的应用程序的lib文件夹下是否有正确的jar持有指定mongo相关的xsds

答案 1 :(得分:0)

我找到了一个解决方案,在 applicationContext.xml 中我只是将一个导入标记放到资源“spring.xml”:

<import resource="spring.xml" />

spring.xml 中:

<!-- MongoDB host -->
<mongo:mongo host="localhost" port="27017" />

<!-- Template for performing MongoDB operations -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" c:mongo-ref="mongo" c:databaseName="dbname" />

<!-- Activate Spring Data MongoDB repository support -->
<mongo:repositories base-package="it.domain" mongo-template-ref="mongoTemplate" />

<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 

现在它开始没有tomcat的崩溃;如果db的连接能正常工作,我会告诉你。