2017-04-20 49 views
0

我是SpringFramework的新手,我下载了tutorialPoint,使用(Java SE8)设置Eclipse并下载了SpringFrameWork 4.3.8以运行HelloWorld示例。我已经在这个问题上做了大量的搜索,但没有发布在互联网上的解决方案解决了我的问题。Spring Hello World - 找不到元素'beans'的声明

我beans.xml的情况如下:

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

 
<beans xmlns="http://springframework.org/schema/beans" 
 
\t \t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t \t xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> \t 
 

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

 
</beans>

下面是执行时的错误消息:

2017年4月20日上午11时14分59秒组织.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

I NFO:刷新org[email protected]5d6d97:启动日期[Thu Apr 20 11:14:59 PDT 2017];上下文层次的根

二零一七年四月二十日上午十一时14分59秒org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO:加载XML豆从类路径资源定义[beans.xml中]

线程“main”中的异常org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自类路径资源[Beans.xml]的XML文档中的第5行无效;嵌套异常是org.xml.sax.SAXParseException; lineNumber:5; columnNumber: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)...

+0

如果你正在做一个教程春春4+,我想你应该找到一个可以使用代码 - 仅限于bean声明,而不是XML文件。过去10年来我一直使用Spring,但过去4年来我没有使用XML定义的bean。 –

+0

Klaus Groenbaek - 感谢您的建议。 – user1941319

回答

0

尝试加入www.到您的xmlns

变化

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

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

哦!我查了一百万次文件,但没有发现失踪的www。谢谢回复。 – user1941319

+0

不用担心。我使用Spring Tool Suite(基于eclipse)。它会生成spring xml文件。我使用kdiff3与您的文件做了差异。我可能不会在没有kdiff3的情况下发现它。你可能应该得到STS或者安装一个插件来帮助用eclipse来管理spring。 –

+0

Titi - 当我开始学习某些东西时,我想手动做所有事情:)。当Spring更严肃地开发时,肯定会使用Tool Suite。 – user1941319

0

更新你的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> 
+0

感谢您的回复!得到它了。 – user1941319

0

您可以通过SpringInitializr

初始化Spring应用程序下载.zip文件后,将其解压缩,转到目录,只需键入

MVN春季启动:运行

它会引导你春季应用。

相关问题