Pregunta

I'm newly at Spring. I tried to write easy project and stack with weird exception FileNotFoundException.

To my mind it is some problem with loading app-context.xml. But I sure that set correct absolute path.

Any suggestions?

Main code:

package com.prospring.ch2;

public class HelloWorldSpringDI {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml");
        MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
        mr.render();
    }
}

Here content of app-context.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="provider" class="com.prospring.ch2.HelloWorldMessageProvider" />
    <bean id="rendere" class="com.prospring.ch2.StandartOutMessageRenderer" 
        p:messageProvider-ref="provider" />

    <!--<description>Example configuration to get you started.</description --> 
    <!-- context:component-scan base-package="com.prospring.ch2" />  -->
</beans>

snippet of console message:

14:24:06,360  INFO t.support.ClassPathXmlApplicationContext: 456 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3f62e847: startup date [Wed Nov 13 14:24:06 EET 2013]; root of context hierarchy
14:24:06,509  INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean definitions from class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml] cannot be opened because it does not exist

I checked path to app-context.xml from terminal all looks ok:

nazar_art@nazar-desktop:~/workspace/ch2/src/main/resources/META-INF/spring$ pwd
/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring
nazar_art@nazar-desktop:~/workspace/ch2/src/main/resources/META-INF/spring$ ls -lg
total 4
-rw-rw-r-- 1 nazar_art 867 Nov 13 14:25 app-context.xml

Here is looking of my struckture:

spring project struckture

  • How to solve this trouble?
¿Fue útil?

Solución

You are using the class path application context loader ClassPathXmlApplicationContext and passing an absolute path to it.

You need to use FileSystemXmlApplicationContext

If you want to use ClassPathXmlApplicationContext then you have to make sure your app-context.xml is in the class path of your application and pass the appropriate resource name, which is different from the absolute path on a filesystem.

Otros consejos

Use either

public static void main(String[] args) {
    ApplicationContext ctx = new FileSystemXmlApplicationContext(
            "/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml");
    ...
}

or

public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
            "META-INF/spring/app-context.xml");
  ...
}

FileSystemXmlApplicationContext

ClassPathXmlApplicationContext

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top