0
votes

I'm trying to learn Spring3 by following book Pro Spring3. I'm in chapter 4 where the author explains IoC and DI. He uses GenericXmlApplicationContext to specify the configuration file. He does the following:

package com.apress.prospring3.ch4;

import org.springframework.context.support.GenericXmlApplicationContext;

public class UsingSetterInjection {

    public static void main(String[] args) {

        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        //ctx.load("classpath:app-context-xml.xml");
        ctx.load("classpath:app-context-annotation.xml");
        ctx.refresh();      

        MessageRenderer messageRenderer = ctx.getBean("messageRenderer", MessageRenderer.class);
        messageRenderer.render();
    }
}

My structure is

src/main/resources/app-context-annotation.xml

IOException parsing XML document from class path resource [app-context-annotation.xml]; nested exception is java.io.FileNotFoundException: class path resource [app-context-annotation.xml] cannot be opened because it does not exist

2
Did you create a Spring configuration file named "app-context-xml.xml"? If so, make sure you've added it to the classpath of your applicationkyiu
My structure is src/main/resources/app-context-annotation.xmlMajid Kamal
First, learn to accept answer! Second, I bet you are using Maven. Check if there is target/classes/app-context-annotation.xml. If not, then you have not even compiled your codeAdrian Shum

2 Answers

1
votes

Give the complete path and it will work like a charm.

    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.load("classpath:META-INF/spring/app-context-annotation.xml");
0
votes

Can you try using the following line to load your xml file?

ctx.load("classpath*:app-context-annotation.xml");

I have added a * after the classpath. I think this should resolve your problem. Otherwise you need to check whether this file is there in the application classpath or not.

Cheers.