简体   繁体   中英

spring class path resource cant be found because it doesnt exist

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

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

and the project structure look like this 在此处输入图片说明

You can try what the Vaibs suggested in the linked answer, but for that you would have to move the root-context.xml in src/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");

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM