Simple Spring -Maven HelloWorld & # 8594; not appContext.xml found?

I'm having some difficulties with the simplest maven-w760 application. In fact, it BeanFactorydoes not find the file appContext.xml, despite the fact that it is in the resource directory and correctly copied to the target directory. Here is my main class, dir structure and 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();

    }
}

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?

+3
source share
2 answers

Thank you, this hint did it. Did not understand that FileInputStream could be a problem. However, now I am using ApplicationContext and it works:

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

Thanks and greetings

+4
source

, XML /classes, , CLASSPATH. getResourceAsStream() , InputStream FileInputStream.

+2

Source: https://habr.com/ru/post/1751111/


All Articles