找不到spring classpath资源,因为它不存在

时间:2014-11-21 07:35:28

标签: java spring maven spring-mvc

我使用我的第一个春天helloworld程序使用maven使用maven这是一个简单的IOC示例

public class Soloapp {
    String solo;
    public Soloapp() {
        // TODO Auto-generated constructor stub
    }
    public String getSolo() {
        return solo;
    }
    public void setSolo(String solo) {
        this.solo = solo;
    }

}

和实现类

package com.solo.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Soloimp {

    public static void main(String[] args) {
        ApplicationContext ap= new ClassPathXmlApplicationContext("/WEB-INF/spring/root-context.xml");
        Soloapp apa=  (Soloapp) ap.getBean("solo");
System.out.println(apa.getSolo());


    }

}

并获得异常

1

og4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/spring/root-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring/root-context.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)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.solo.spring.Soloimp.main(Soloimp.java:9)

它显示无法找到xml文件位置,但我指定了我使用弹簧工具套件mvc maven项目的位置

,项目结构如下enter image description here

2 个答案:

答案 0 :(得分:2)

您可以尝试Vaibs在链接答案中建议的内容,但为此您必须移动root-context.xml中的src/main/resources

如果您想保留它,请按照

加载它
ConfigurableApplicationContext ap = new ClassPathXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/root-context.xml");

答案 1 :(得分:0)

如果我没错,当从STS / Eclipse运行时,路径将从项目顶部解决。因此,尝试从根目录设置文件的路径,即src / main / webapp / WEB-INF ....