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>
应该怎么修改
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>
应该怎么修改
你用ClassPathResource的话仅仅在Classpath路径下找的
默认的ClassPath是从src下开始找到。