类一:
package onlyfun.caterpillar;
public class HelloBean {
private String helloWord;
public void setHelloWord(String helloWord){
this.helloWord=helloWord;
}
public String getHelloWord(){
return helloWord;
}
}
类二:
package onlyfun.caterpillar;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class SpringDemo {
public static void main(String[] args){
Resource rs=new ClassPathResource("beans-config.xml");//获取配置文件来源;
BeanFactory factory=new XmlBeanFactory(rs);
HelloBean bean=(HelloBean)factory.getBean("helloBean");
System.out.println(bean.getHelloWord());
}
}
配置文件:(名字为Beans-config.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.spring.framework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloBean" class="onlyfun.caterpillar.helloBean">
<property name="helloWord">
<value>"HelloWorld!"</value>
</property>
</bean>
</beans>
但运行类二时,却出现如下错误提示:
2007-10-10 15:25:53 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-config.xml]
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 5 in XML document from class path resource [beans-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
---------------------------------------------------------------
请各位指点下老弟。谢谢。
------解决方案--------------------
<bean id="helloBean" class="onlyfun.caterpillar.helloBean" >
class应该为onlyfun.caterpillar.HelloBean h应该大写,我已经调过了,第一次回答问题,很高兴,呵呵