Spring无法找到classpath资源--->类路径资源因为不存在而无法打开

时间:2014-08-22 10:47:55

标签: spring

Spring无法找到classpath资源。(welcomeUser.xml)

ClassPathXmlApplicationContext appContext=new ClassPathXmlApplicationContext("welcomeUser.xml");

welcomeUser.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-3.0.xsd">

<bean id="welcomeUserBean" class="bean.WelcomeUserBean">  
<property name="happyHourService">       
    <ref bean="happyHourService"/>        
</property>
<property name="standardService">       
    <ref bean="standardService"/>             
</property>
</bean>

</beans>

例外:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [../welcomeUser.xml]; nested exception is java.io.FileNotFoundException: class path resource [../welcomeUser.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209).....................................

无法摆脱这种异常......需要帮助

1 个答案:

答案 0 :(得分:0)

您正在尝试使用类路径样式加载“welcomeUser.xml”,因此请确保“welcomeUser.xml”在哪里?如果它在WEB-INF或WEB-INF / classes下,请使用:

ClassPathXmlApplicationContext appContext=new ClassPathXmlApplicationContext("/welcomeUser.xml");

如果它不存在,请告诉我,让我告诉你如何写这条路!