Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Spring-Maven HelloWorld -> appContext.xml not found?

Tags:

spring

I am having some difficulties with the simplest maven-spring app. Actually, BeanFactory does not find the appContext.xml file, despite it is located in the resources dir and copied properly into the target dir. Here's my main class, the dir structure and the error:

// MAIN CLASS
package sas.test.spring;

import java.io.FileInputStream;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello World!");

        BeanFactory fact = new XmlBeanFactory((Resource) new FileInputStream("appContext.xml"));

        GreetingService gs = (GreetingService) fact.getBean("greetingService");

        gs.sayGreeting();

    }
}

The directory structure after mvn compile:

testspring002
|--src
|  `--main
|     |--java
|     |  `--sas
|     |     `--test
|     |        `--spring
|     |           |--App.java
|     |           |--GreetingService.java
|     |           `--GreetingServiceImpl.java
|     `--resources
|        `--appContext.xml
...
`--target
   `--classes
      |--appContext.xml
      `--sas
         `--test
            `--spring
               |--App.class
               |--GreetingService.class
               `--GreetingServiceImpl.class

And here is the error:

/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java -Dclassworlds.conf=/usr/share/java/maven-2.2.0/bin/m2.conf -Dmaven.home=/usr/share/java/maven-2.2.0 -Dfile.encoding=MacRoman -classpath /usr/share/java/maven-2.2.0/boot/classworlds-1.1.jar org.codehaus.classworlds.Launcher --no-plugin-registry --fail-fast --no-plugin-updates --strict-checksums --update-snapshots -f /Users/sas/Development/workspace_idea/testspring002/pom.xml org.codehaus.mojo:exec-maven-plugin:1.1.1:java
+ Enabling strict checksum verification on all artifact downloads.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building testspring002
[INFO]    task-segment: [org.codehaus.mojo:exec-maven-plugin:1.1.1:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] artifact org.codehaus.mojo:exec-maven-plugin: checking for updates from central
[INFO] No goals needed for project - skipping
[INFO] [exec:java {execution: default-cli}]
Hello World!
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

appContext.xml (No such file or directory)

Any suggestions?

like image 703
Ta Sas Avatar asked Aug 14 '26 16:08

Ta Sas


2 Answers

thanks, that hint did it. Didn't realize that FileInputStream might be an issue. However, I use ApplicationContext now and it works:

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"appContext.xml"});

Thanks & Cheers

like image 159
Ta Sas Avatar answered Aug 17 '26 13:08

Ta Sas


Yes, since the app context XML is in /classes, that means it's in the CLASSPATH. I'd recommend calling getResourceAsStream() from the class loader to get the InputStream instead of FileInputStream.

like image 31
duffymo Avatar answered Aug 17 '26 14:08

duffymo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!