gpt4 book ai didi

spring - 简单的 Spring-Maven HelloWorld -> appContext.xml 未找到?

转载 作者:行者123 更新时间:2023-12-02 09:59:30 24 4
gpt4 key购买 nike

我在使用最简单的 maven-spring 应用程序时遇到了一些困难。实际上,BeanFactory 找不到 appContext.xml 文件,尽管它位于资源目​​录中并正确复制到目标目录中。这是我的主类、目录结构和错误:

// 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();

}
}

mvn编译后的目录结构:

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

这是错误:

/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)

有什么建议吗?

最佳答案

谢谢,这个提示做到了。没有意识到 FileInputStream 可能是一个问题。但是,我现在使用 ApplicationContext 并且它有效:

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

感谢与干杯

关于spring - 简单的 Spring-Maven HelloWorld -> appContext.xml 未找到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3089436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com