2014-07-05 42 views
0

解析错误我试图使用spring数据elasticsearch模块,但在定义我的bean时,我在spring-elasticsearch.xsd中得到了一个解析错误。解析弹簧springsearch.xsd

空间声明如下:

<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:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
      http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd"> 

,我得到这个语法错误:如果我下载的文件,并从中按预期工作标签删除源属性

SEVERE: Caught exception while allowing TestExecutionListener [or[email protected]795ee430] to prepare test instance [[email protected]] 
java.lang.IllegalStateException: Failed to load ApplicationContext 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) 
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122) 
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105) 
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    at org.junit.runners.Suite.runChild(Suite.java:127) 
    at org.junit.runners.Suite.runChild(Suite.java:26) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 40 in XML document from class path resource [dao-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd; lineNumber: 40; columnNumber: 116; s4s-att-invalid-value: Invalid attribute value for 'source' in element 'documentation': cvc-datatype-valid.1.2.1. 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:233) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:117) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100) 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248) 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64) 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) 
    ... 36 more 
Caused by: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd; lineNumber: 40; columnNumber: 116; s4s-att-invalid-value: Invalid attribute value for 'source' in element 'documentation': cvc-datatype-valid.1.2.1. 
    at org.apache.xerces.parsers.DOMParser.parse(DOMParser.java:267) 
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:201) 
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) 
    ... 49 more 

这是因为我还在使用spring 3.2,还是有一些其他的错误,我在做什么?

回答

2

最近我偶然发现了这个问题,我有一个解决方法,它不是很漂亮(并且不使用elasticsearch命名空间),但使用了包装的FactoryBean对象)。

首先删除的xmlns和参考的schemaLocation的elasticsearch XSD然后引用TransportClientFactoryBean作为一个bean:

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

    <!-- Elasticsearch client --> 
    <bean id="esClient" class="org.springframework.data.elasticsearch.client.TransportClientFactoryBean"> 
    <property name="clusterNodes" value="localhost:9300" /> 
    <property name="clusterName" value="mycluster" /> 
    </bean> 

现在你可以使用esClient bean作为TransportClient

+0

感谢您的回答,我的解决方法是修复在XSD文件中incompability,并将其放置在资源目录,用类路径而不是http引用它。 –

+0

你能分享这个答案吗?具体如何“用classpath而不是http引用它”。我相信别人会觉得它很有用 –

1

在回应@克里斯白。

在我的情况下,我已经在META-INF中存储了xsd文件。

1)下载de elasticsearch-1.0.xsd并正确修复。

<xsd:schema xmlns="http://www.springframework.org/schema/data/elasticsearch" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" xmlns:repository="http://www.springframework.org/schema/data/repository" targetNamespace="http://www.springframework.org/schema/data/elasticsearch" elementFormDefault="qualified" attributeFormDefault="unqualified"> 
    <xsd:import namespace="http://www.springframework.org/schema/beans"/> 
    <xsd:import namespace="http://www.springframework.org/schema/tool"/> 
    <xsd:import namespace="http://www.springframework.org/schema/data/repository" schemaLocation="http://www.springframework.org/schema/data/repository/spring-repository.xsd"/> 
    <xsd:element name="repositories"> 
     <xsd:complexType> 
      <xsd:complexContent> 
       <xsd:extension base="repository:repositories"> 
        <xsd:attributeGroup ref="repository:repository-attributes"/> 
        <xsd:attribute name="elasticsearch-template-ref" type="elasticsearchTemplateRef" default="elasticsearchTemplate"/> 
       </xsd:extension> 
      </xsd:complexContent> 
     </xsd:complexType> 
    </xsd:element> 
    <xsd:simpleType name="elasticsearchTemplateRef"> 
     <xsd:annotation> 
      <xsd:appinfo> 
       <tool:annotation kind="ref"> 
        <tool:assignable-to type="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"/> 
       </tool:annotation> 
      </xsd:appinfo> 
     </xsd:annotation> 
     <xsd:union memberTypes="xsd:string"/> 
    </xsd:simpleType> 
    <xsd:element name="node-client"> 
     <xsd:annotation> 
      <xsd:documentation /> 
      <xsd:appinfo> 
       <tool:assignable-to type="org.elasticsearch.client.Client"/> 
      </xsd:appinfo> 
     </xsd:annotation> 
     <xsd:complexType> 
      <xsd:complexContent> 
       <xsd:extension base="beans:identifiedType"> 
        <xsd:attribute name="local" type="xsd:boolean" default="false"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[local here means local on the JVM (well, actually class loader) level, meaning that two local servers started within the same JVM will discover themselves and form a cluster]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
        <xsd:attribute name="cluster-name" type="xsd:string" default="elasticsearch"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[Name of the cluster in which this instance of node client will connect to]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
        <xsd:attribute name="http-enabled" type="xsd:boolean" default="true"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[ to enable or desable http port ]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
       </xsd:extension> 
      </xsd:complexContent> 
     </xsd:complexType> 
    </xsd:element> 
    <xsd:element name="transport-client"> 
     <xsd:annotation> 
      <xsd:documentation /> 
      <xsd:appinfo> 
       <tool:assignable-to type="org.elasticsearch.client.Client"/> 
      </xsd:appinfo> 
     </xsd:annotation> 
     <xsd:complexType> 
      <xsd:complexContent> 
       <xsd:extension base="beans:identifiedType"> 
        <xsd:attribute name="cluster-nodes" type="xsd:string" default="127.0.0.1:9300"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[The comma delimited list of host:port entries to use for elasticsearch cluster.]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
        <xsd:attribute name="cluster-name" type="xsd:string" default="elasticsearch"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[Name of the cluster in which this instance of node client will connect to]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
        <xsd:attribute name="client-transport-sniff" type="xsd:boolean" default="true"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[The client allows to sniff the rest of the cluster, and add those into its list of machines to use.]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
        <xsd:attribute name="client-transport-ignore-cluster-name" type="xsd:boolean" default="false"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[Set to true to ignore cluster name validation of connected nodes. (since 0.19.4)]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
        <xsd:attribute name="client-transport-ping-timeout" type="xsd:string" default="5s"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[The time to wait for a ping response from a node. Defaults to 5s.]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
        <xsd:attribute name="client-transport-nodes-sampler-interval" type="xsd:string" default="5s"> 
         <xsd:annotation> 
          <xsd:documentation> 
           <![CDATA[How often to sample/ping the nodes listed and connected. Defaults to 5s.]]> 
          </xsd:documentation> 
         </xsd:annotation> 
        </xsd:attribute> 
       </xsd:extension> 
      </xsd:complexContent> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

2)存放在/ webapp的/ META-INF/

3)参考它的schemaLocation和他的定义正常工作。

<?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:es="http://www.springframework.org/schema/data/elasticsearch" 
     xsi:schemaLocation=" http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/data/elasticsearch 
          META-INF/elasticsearch-1.0.xsd"> 

    <es:repositories base-package="com.project.repositories" /> 

    <es:transport-client id="searchClient" cluster-nodes="localhost:9300" /> 

    <bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> 
     <constructor-arg name="client" ref="searchClient"/> 
    </bean> 

</beans> 
0

感谢Dani,这工作。最近该修复已被Artur应用。

请参考:https://jira.spring.io/browse/DATAES-103

所以,直到下一个版本出来了,你可以如下使用:

xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch https://jira.spring.io/secure/attachment/22773/spring-elasticsearch-1.0-fixed.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">