1

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 enter image description here

javaworld
  • 427
  • 1
  • 9
  • 19
  • This would help rather then explaining. http://stackoverflow.com/questions/12893760/spring-cannot-find-bean-xml-configuration-file-when-it-does-exist – Vaibs Nov 21 '14 at 07:57
  • 1
    WEB-INF isn't part of the classpath, so naturally it will fail. Only `src/main/resources` and `src/main/java` is part of the classpath, `src/main/webapp` isn't. – M. Deinum Nov 21 '14 at 09:08

2 Answers2

2

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");
Master Slave
  • 27,771
  • 4
  • 57
  • 55
0

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

Nadir
  • 1,369
  • 1
  • 15
  • 28