} } }

    Spring 异常 —— cvc-elt.1: Cannot find the declaratio

    添加时间:2013-7-31 点击量:

    有个应用 Spring 的项目,运行时报错:



     1 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 5 in XML document  class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element beans.
    
    2 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404
    3 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342
    4 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310
    5 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143
    6 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178
    7 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149
    8 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212
    9 at org.springframework.test.AbstractSingleSpringContextTests.createApplicationContext(AbstractSingleSpringContextTests.java:212
    10 at org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations(AbstractSingleSpringContextTests.java:189
    11 at org.springframework.test.AbstractSingleSpringContextTests.loadContext(AbstractSingleSpringContextTests.java:169
    12 at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:140
    13 at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:98
    14 at junit.framework.TestCase.runBare(TestCase.java:132
    15 at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76
    16 at junit.framework.TestResult¥1.protect(TestResult.java:110
    17 at junit.framework.TestResult.runProtected(TestResult.java:128
    18 at junit.framework.TestResult.run(TestResult.java:113
    19 at junit.framework.TestCase.run(TestCase.java:124
    20 at junit.framework.TestSuite.runTest(TestSuite.java:243
    21 at junit.framework.TestSuite.run(TestSuite.java:238
    22 at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83
    23 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50
    24 at org.eclipse.jdt.internal.junit.runner.Testution.run(Testution.java:38
    25 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467
    26 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683
    27 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390
    28 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197
    29 Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element beans.
    30 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195
    31 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131
    32 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384
    33 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318
    34 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1887
    35 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685
    36 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400
    37 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl¥NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626
    38 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl¥FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3095
    39 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl¥PrologDriver.next(XMLDocumentScannerImpl.java:921
    40 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648
    41 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140
    42 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510
    43 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807
    44 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737
    45 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107
    46 at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225
    47 at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283
    48 at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75
    49 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396
    50 ... 26 more



    该项目在别的一台机械上是可以运行的。


    后来网上查了查,是bean文件的头有题目。引入的是 Spring 2.5 的包,但文件头倒是用 Spring 3.0


    原文件:



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


    后来改成 Spring 2.5 包里某个例子的文件头,就可以运行了。


    批改之后:



    1 <?xml version=1.0 encoding=UTF-8?>
    
    2 <beans xmlns=http://www.springframework.org/schema/beans
    3 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
    4 xsi:schemaLocation=http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    5 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    6 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd>



    首要差别在于这个属性的值 xsi:schemaLocation





    ?疑问


    为什么同样的代码,在这个机械上可以运行,而在别的一台却不可呢?



    ¿ 猜想


    两台机械大不合就是,一台可以联网,一台不可。


    而雷同的代码,之所以在可以联网那台机械上可以运行,是因为当本地找不到 xsd 文件时,它会主动上彀找。

    原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
    分享到: