本人刚开始学习spring,问题如下:
1.HelloBean类:
package ioc1;public class HelloBean {
private String user;
private int salary;

public String sayHello(){
return "hello:"+user+"salary:"+salary;
}
public void setUser(String user){
this.user=user;
}
public void setSalary(int salary){
this.salary=salary;
}}
2.applicationgContext.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:shcemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
   <bean id="helloBean" class="ioc1.HelloBean">
       <property name="user">
           <value>zhangshan</value>
       </property>
       <property name="salary" value="2000"/>
   </bean>
</beans>3。Test测试类:
package ioc1;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 Test {
public static void main(String[] args){
Resource rs=
new ClassPathResource("F:/Test/SpringTest/src/applicationContext.xml");
BeanFactory bf=
new XmlBeanFactory(rs);
HelloBean hb=(HelloBean)bf.getBean("helloBean");
System.out.println(hb.sayHello());
HelloBean hb2=(HelloBean)bf.getBean("helloBean");
System.out.println(hb==hb2);
}}
4.运行后出异常:
2012-8-24 15:44:02 org.springframework.core.CollectionFactory <clinit>
信息: JDK 1.4+ collections available
2012-8-24 15:44:02 org.springframework.core.CollectionFactory <clinit>
信息: Commons Collections 3.x available
2012-8-24 15:44:02 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ioc1/applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Line 6 in XML document from class path resource [ioc1/applicationContext.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'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(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.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(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.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:352)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:277)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at ioc1.Test.main(Test.java:13)

解决方案 »

  1.   

    applicationContext.xml路径有问题Resource rs = new ClassPathResource("applicationContext.xml");
    直接这样
      

  2.   

    applicationgContext.xml文件:
    <beans
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:shcemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">改成<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        
           classpath:/org/springframework/beans/factory/xml/spring-beans-2.0.xsd">
      

  3.   


    原因是可能你的网络的原因 不能访问到http://www.springframework.org/schema/beans/spring-beans-2.0.xsd所以全部换成本地jar包中的路径 就行了
      

  4.   

    若你的applicationContext.xml放在src下面,在web.xml下是要加上这么一段<context-param>
    <param-name>ContextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    <context-param/>