public class Test {
    public static void main(String[] args){
        ClassPathResource resource = new ClassPathResource("/home/qiucheng/NetBeansProjects/sh/web/WEB-INF/bean-config.xml");        BeanFactory factory = new XmlBeanFactory(resource);        Hello h = (Hello) factory.getBean("hello");        System.out.println("h.getHello::"+h.getHelloWord());
    }
}
运行报错:::
May 26, 2009 11:22:03 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [home/qiucheng/NetBeansProjects/sh/web/WEB-INF/bean-config.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [home/qiucheng/NetBeansProjects/sh/web/WEB-INF/bean-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [home/qiucheng/NetBeansProjects/sh/web/WEB-INF/bean-config.xml] cannot be opened because it does not exist
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:320)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:290)
        at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
        at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
        at com.cn.hs.main.Test.main(Test.java:21)
Caused by: java.io.FileNotFoundException: class path resource [home/qiucheng/NetBeansProjects/sh/web/WEB-INF/bean-config.xml] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:307)
        ... 4 more
Java Result: 1
bean-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.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="hello" class="com.cn.hs.pojo.Hello">
        <property name="helloWord">
            <value>ni hao sa</value>
        </property>
    </bean>
</beans>
应该怎么修改

解决方案 »

  1.   

     "/home/qiucheng/NetBeansProjects/sh/web/WEB-INF/bean-config.xml" 这个不是绝对路径,lz搞错了吧.
      

  2.   

    实在不行 建议将spring的配置文件放在src下 这样的话在上面的代码中("bean-config.xml") 就可以获得该配置文件了
      

  3.   

    如果用绝对路径的话就用FileSystemResouce这个类。
    你用ClassPathResource的话仅仅在Classpath路径下找的
      

  4.   

    用ClassPathResource的话,就应该把配置文件放在src目录下。
    默认的ClassPath是从src下开始找到。