29

我有弹簧罐,spring-3.2.0.RC1.jar并试图从这里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

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

4

13 回答 13

45

试试这个,假设你在 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">

用您使用的主要Spring 版本替换3.1最后一行。含义:即使有 Spring 版本,也没有XSD 。3.1.13.1.1

于 2012-12-11T06:12:37.520 回答
7

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

在这种情况下,从 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">
于 2014-02-06T07:06:27.983 回答
4

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

<?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>
于 2017-01-03T07:32:56.917 回答
1

此错误找不到元素“bean”的声明,但出于完全不同的原因

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

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/

于 2015-09-16T21:52:51.943 回答
1

我遇到了这个问题,根本原因是在 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">
于 2016-03-15T16:35:21.027 回答
0

尝试使用这个 - 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"> 
于 2018-04-27T09:20:50.857 回答
0

确保构建路径中所有 spring jar 文件的版本与 xml 文件中提到的版本相同。

于 2019-04-02T06:52:28.157 回答
0

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

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

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

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

于 2020-06-05T13:30:46.593 回答
0

对我来说,问题是 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,它对我有用。

于 2020-07-01T12:56:20.180 回答
0

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

  1. 重新配置文件:Spring 配置文件(不使用 XSD 命名空间声明)。 2. -> 主要目的是没有标签名称(bean,...)
  2. 使用您要使用的类创建标签。 在此处输入图像描述 ---快乐编码---
于 2021-03-28T14:38:46.360 回答
0

尝试使用这个:

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">
于 2021-04-25T16:17:37.093 回答
0

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

<? 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>
于 2022-01-27T10:30:10.960 回答
-4

使用它来解决您的问题:

<context:annotation-config/>
于 2016-10-12T15:00:54.990 回答