【问题标题】:Spring Hello World - Cannot find the declaration of element 'beans'Spring Hello World - 找不到元素“bean”的声明
【发布时间】:2017-09-17 13:21:14
【问题描述】:

我是 SpringFramework 的新手,我下载了 tutorialPoint 上的教程,使用 (Java SE8) 设置了 Eclipse,并下载了 SpringFrameWork 4.3.8 来运行 HelloWorld 示例。我对这个问题进行了广泛的搜索,但互联网上发布的解决方案都没有解决我的问题。

我的 Beans.xml 如下:

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

<beans xmlns="http://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-4.3.xsd">	

<bean id="helloWorld" class="com.spring.SpringHello">
	<property name="message" value="Hello Spring World!" />
</bean>		

</beans>

以下是执行时的错误信息:

2017 年 4 月 20 日上午 11:14:59 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

INFO:刷新 org.springframework.context.support.ClassPathXmlApplicationContext@5d6d97:启动日期 [Thu Apr 20 11:14:59 PDT 2017];上下文层次结构的根

2017 年 4 月 20 日上午 11:14:59 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO:从类路径资源 [Beans.xml] 加载 XML bean 定义

线程“main”中的异常 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自类路径资源 [Beans.xml] 的 XML 文档中的第 5 行无效;嵌套异常是 org.xml.sax.SAXParseException;行号:5;列号:134; cvc-elt.1:找不到元素“beans”的声明。

在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)

在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) ...

【问题讨论】:

  • 如果您正在为 Spring 4+ 编写 Spring 教程,我认为您应该找到使用纯代码 bean 声明而不是 XML 文件的教程。过去 10 年我一直使用 Spring,但过去 4 年我没有使用 XML 定义的 bean。
  • Klaus Groenbaek - 感谢您的建议。

标签: spring


【解决方案1】:

尝试将www. 添加到您的 xmlns

改变

xmlns="http://springframework.org/schema/beans"

xmlns="http://www.springframework.org/schema/beans"

【讨论】:

  • 哦!我检查了文件一百万次,但没有发现丢失的 www。感谢您的回复。
  • 不用担心。我使用 Spring Tool Suite(基于 Eclipse)。它生成spring xml文件。我使用 kdiff3 对您的文件进行了比较。如果没有 kdiff3,我可能不会发现它。您可能应该获得 STS 或安装插件来帮助使用 eclipse 管理 spring。
  • Titi - 当我开始学习一些东西时,我想手动做所有事情:)。使用 Spring 进行更认真的开发时,肯定会使用 Tool Suite。
【解决方案2】:

您可以通过SpringInitializr初始化Spring应用程序

下载 .zip 文件后,解压,转到目录并输入

mvn spring-boot:run

它将启动您的 spring 应用程序。

【讨论】:

    【解决方案3】:

    将您的 XML 文件更新为

    <?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-4.3.xsd         
                           ">
    
        <bean id="helloWorld" class="com.spring.SpringHello">
            <property name="message" value="Hello Spring World!" />
        </bean>     
    </beans>
    

    【讨论】:

    • 感谢您的回复!明白了。
    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2014-02-23
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多