java 找不到 spring 类路径资源,因为它不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27056319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:07:41  来源:igfitidea点击:

spring class path resource cant be found because it doesnt exist

javaspringmavenspring-mvc

提问by javaworld

im using my first spring helloworld program using STS using maven itwas a simple IOC example

我使用我的第一个 spring helloworld 程序使用 STS 使用 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;
    }

}

and the implementation class

和实现类

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());


    }

}

and getting the exception

并得到例外

l

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)

it was showing cant find the xml file location but i specified the location i was using spring tool suite mvc maven project

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

and the project structure look like this enter image description here

和项目结构看起来像这样 在此处输入图片说明

回答by Master Slave

You can try what the Vaibs suggested in the linked answer, but for that you would have to move the root-context.xmlin src/main/resources.

你可以尝试一下Vaibs在链接的答案建议,但你必须移动root-context.xmlsrc/main/resources

If you want to keep it in place, load it like this

如果要保持原位,请像这样加载

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

回答by Nadir

If I'm not wrong, when running from STS/Eclipse the path is resolved from the top of the project. So try to set the path to the file from the root ie src/main/webapp/WEB-INF....

如果我没记错的话,当从 STS/Eclipse 运行时,路径是从项目顶部解析的。所以尝试从根目录设置文件的路径,即 src/main/webapp/WEB-INF ....