【问题标题】:Cannot find the declaration of element 'beans'找不到元素“bean”的声明
【发布时间】:2012-11-28 15:44:05
【问题描述】:

我有spring-3.2.0.RC1.jar 的弹簧罐,并尝试从给定here 的教程中实现Apache ActiveMQ helloWorld 程序。 xml配置文件为:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemalocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jms 
    http://www.springframework.org/schema/jms/spring-jms.xsd">

    <bean class="org.apache.activemq.command.ActiveMQQueue" id="destination">
        <constructor-arg value="TEST.Q1"></constructor-arg>
    </bean>

    <bean class="org.apache.activemq.ActiveMQConnectionFactory" id="connectionFactory"
        p:brokerurl="tcp://localhost:8161"></bean>

    <bean class="com.jms.helloworld.listner.MessageListenerImpl" id="simpleMessageListener">
        <bean
            class="org.springframework.jms.listener.DefaultMessageListenerContainer"
            id="jmsContainer">
            <property name="connectionFactory" ref="connectionFactory"></property>
            <property name="destination" ref="destination"></property>
            <property name="messageListener" ref="simpleMessageListener"></property>
        </bean>
    </bean>
</beans>    

主要:

public static void main(String[] args) throws JMSException {
        System.out.println("LISTNER STARTED");
        ApplicationContext context =  new FileSystemXmlApplicationContext("src/com/jms/helloworld/config/JMSConfig.xml");
    }

**Exception on console**


LISTNER STARTED
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from file [/home/neal/workspace/ActiveMQListener/src/com/jms/helloworld/config/JMSConfig.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 61; cvc-elt.1: Cannot find the declaration of element 'beans'.
    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.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at com.jms.helloworld.test.TestJMSListner.main(TestJMSListner.java:15)
Caused by: org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 61; cvc-elt.1: Cannot find the declaration of element 'beans'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:387)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:321)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1920)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:709)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3080)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:899)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:625)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:819)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:748)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:288)
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
    ... 14 more

我不知道豆子出了什么问题。到目前为止,其他问题中的任何建议都无济于事。有什么帮助吗?

【问题讨论】:

  • xml 验证器抱怨 不是有效元素。这在外部 xml 站点中验证。你能从你所在的地方打开springframework.org/schema/beans/spring-beans.xsd吗?在本地复制它有帮助吗?可能是防火墙问题。
  • 可能是这个原因 [getting-error-message-with-spring-cvc-elt-1-cannot-find-the-declaration-of-ele][1] [1]: @987654323 @

标签: java spring activemq message-queue


【解决方案1】:

我使用以下代码解决了这个问题。

<? 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:jee="http://www.springframework.org/schema/jee" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

</Beans>

【讨论】:

    【解决方案2】:

    试试这个:

    xsi:schemaLocation="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">
    

    【讨论】:

      【解决方案3】:

      我也遇到了和你一样的情况。 我的解决方案如下:

      1. 重新配置文件:Spring 配置文件(不使用 XSD 命名空间声明)。 -> 主要目的是没有标签名称(bean,...)
      2. 使用您要使用的类创建标签。 ---快乐编码---

      【讨论】:

        【解决方案4】:

        对我来说,问题是 spring 无法下载 http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context/spring-context.xsd

        但是我能够从我的浏览器访问这些,因为它正在使用我的机器代理。所以我只是将两个 xsd 的内容复制到名为 spring-beans.xsd 和 spring-context.xsd 的文件中,并将 http url 替换为文件名,它对我有用。

        【讨论】:

        • 我尝试过的另一件事是,我恢复了我的更改,使我的命名空间与以前一样,没有任何本地 xsd。而是将我的 jar 与代理 args 运行为: java -Dhttp.proxyHost=myProxyHost -Dhttp.proxyPort=myproxyport -jar xyz.jar 它工作正常
        【解决方案5】:

        对我来说,问题是我的文件编码...我使用 powershell 编写 xml 文件,这不是 UTF-8 ... 似乎spring需要UTF8,因为一旦我更改了编码(使用notepad ++),它就会再次工作而没有任何错误

        现在我在我的 powershellscript 中使用以下行以 UTF-8 输出 xml 文件: [IO.File]::WriteAllLines($fname_dataloader_xml_config_file, $dataloader_configfile)

        而不是使用重定向操作符 > 来创建我的文件

        注意:我没有在我的 beans 标签中放入任何 xml 参数,它可以工作

        【讨论】:

          【解决方案6】:

          确认你构建路径中所有spring jar文件的版本和xml文件中提到的版本是否相同。

          【讨论】:

            【解决方案7】:

            尝试使用这个 - Spring 4.0。工作

            <beans xmlns="http://www.springframework.org/schema/beans"
                   xmlns:p="http://www.springframework.org/schema/p"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:context="http://www.springframework.org/schema/context" 
                   xsi:schemaLocation="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"> 
            

            【讨论】:

              【解决方案8】:

              添加此代码..它帮助了我

              <?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:aop="http://www.springframework.org/schema/aop" 
                       xsi:schemaLocation="
                       http://www.springframework.org/schema/beans 
                         classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd 
                       http://www.springframework.org/schema/context 
                       classpath:/org/springframework/context/config/spring-context-3.0.xsd
                      http://www.springframework.org/schema/aop 
                      classpath:/org/springframework/aop/config/spring-aop-3.0.xsd
                       ">      
                   </beans>
              

              【讨论】:

              • 在某些公司内部环境中,访问外部网络获取架构并不总是可行的。在这种情况下,这里的解决方案非常有效。
              • 解决了我们在离线环境中的问题。有点棘手的是,IDEA 会将类路径标记为红色作为未解析的引用,而它实际上工作正常。
              【解决方案9】:

              用它来解决你的问题:

              <context:annotation-config/>
              

              【讨论】:

              • 这与问题无关。
              【解决方案10】:

              我遇到了这个问题,根本原因是 xsi:schemaLocation 中的 www.springframework.org/schema/beans 引用之后的空白(如下所示),即

              <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:p="http://www.springframework.org/schema/p"
              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/mvc
              http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
              

              【讨论】:

                【解决方案11】:

                Cannot find the declaration of element 'beans' but for a whole different reason的这个错误

                原来我的互联网连接不是很可靠,所以我决定先检查这个网址

                http://www.springframework.org/schema/context/spring-context-4.0.xsd

                当我看到 xsd 成功打开后,我清理了 Eclipse(IDE) 项目并且错误消失了

                如果您尝试此步骤后仍然出现错误,那么 检查 Spring 版本,使其与另一个答案提到的匹配

                <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-**[MAYOR.MINOR]**.xsd">
                

                将最后一行的 [MAYOR.MINOR] 替换为您正在使用的任何 major.minor Spring 版本

                对于 Spring 4.0 http://www.springframework.org/schema/context/spring-context-4.0.xsd

                对于 Sprint 3.1 http://www.springframework.org/schema/beans spring-beans-3.1.xsd

                所有上下文都在此处可用 http://www.springframework.org/schema/context/

                【讨论】:

                  【解决方案12】:

                  假设您使用的是 Spring 3.1,试试这个:

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

                  将最后一行的3.1 替换为您使用的任何主要 Spring 版本。含义:没有3.1.1 XSD,即使有Spring版本3.1.1

                  【讨论】:

                  • 明确一点:您必须将您的 spring 库版本与 XSD 版本相匹配。
                  • 查看spring-beans包的org.springframework.beans.factory.xml中的选项。
                  【解决方案13】:

                  在解决我的问题的另一个线程上找到它...正在使用互联网连接较少的网络。

                  在这种情况下,从 url 复制 xsd 文件并将其放在 beans.xml 文件旁边,并将 xsi:schemaLocation 更改为:

                   <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 
                  spring-beans-3.1.xsd">
                  

                  【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2014-02-23
                  • 1970-01-01
                  • 2017-09-17
                  • 2011-12-27
                  • 1970-01-01
                  • 2012-07-02
                  • 2012-09-14
                  相关资源
                  最近更新 更多