Hello.java如下:
package spring.chapter1;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Hello {
private String msg;
public void setMsg(String msg){
this.msg = msg;
}
public void sayHello(){
System.out.println(msg);
}
/**
* @param args
*/
public static void main(String[] args){
Resource res = new ClassPathResource("spring/chapter1/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
Hello hello = (Hello)factory.getBean("helloBean");
hello.sayHello();
}
}
bean.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="spring.chapter1.Hello">
<property name="msg" value="这是一个简单的spring实例!"/>
</bean>
</beans>
却抛出了如下的异常:
2011-1-19 0:04:25 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/chapter1/bean.xml]
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 2 in XML document from class path resource [spring/chapter1/bean.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required after keyword PUBLIC in DOCTYPE decl.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at spring.chapter1.Hello.main(Hello.java:24)
Caused by: org.xml.sax.SAXParseException: White spaces are required after keyword PUBLIC in DOCTYPE decl.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanExternalID(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.scanDoctypeDecl(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
... 5 more
请求高手帮忙
14 个解决方案
#1
bean.xml 写的有问题
#2
Line 2 in XML document from class path resource [spring/chapter1/bean.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required after keyword PUBLIC in DOCTYPE decl.
bean.xml的头部写的有问题,该<!DOCTYPE PUBLIC ....
bean.xml的头部写的有问题,该<!DOCTYPE PUBLIC ....
#3
bean.xml 贴出来看一下。
#4
二楼说的,没错
#5
#6
你把spring/chapter1/bean.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
#7
bean.xml这个文件有问题
#8
3楼说的,我修改了,但是还是出现这种错误,6楼说的,我也修改了,还是不行,不知道错误出现在哪
#9
看你spring是什么版本的
在下载的spring包中拷贝一个xml文件 修改一下呀
#10
ApplicationConctext ac=new ClassPathXmlApplicationContext("applicationContext_hn02.xml");
ac.getbean("hellobean");
试试...
ac.getbean("hellobean");
试试...
#11
<?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-2.0.xsd">
...
</beans>
#12
谢谢各位,11楼的方法我使用后运行成功了
#13
o(*_*)o
#14
感谢11楼啊,我也遇到同样的问题,按照你的方法解决了!!
#1
bean.xml 写的有问题
#2
Line 2 in XML document from class path resource [spring/chapter1/bean.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required after keyword PUBLIC in DOCTYPE decl.
bean.xml的头部写的有问题,该<!DOCTYPE PUBLIC ....
bean.xml的头部写的有问题,该<!DOCTYPE PUBLIC ....
#3
bean.xml 贴出来看一下。
#4
二楼说的,没错
#5
#6
你把spring/chapter1/bean.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
#7
bean.xml这个文件有问题
#8
3楼说的,我修改了,但是还是出现这种错误,6楼说的,我也修改了,还是不行,不知道错误出现在哪
#9
看你spring是什么版本的
在下载的spring包中拷贝一个xml文件 修改一下呀
#10
ApplicationConctext ac=new ClassPathXmlApplicationContext("applicationContext_hn02.xml");
ac.getbean("hellobean");
试试...
ac.getbean("hellobean");
试试...
#11
<?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-2.0.xsd">
...
</beans>
#12
谢谢各位,11楼的方法我使用后运行成功了
#13
o(*_*)o
#14
感谢11楼啊,我也遇到同样的问题,按照你的方法解决了!!