Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Maven exec找不到Spring XML文件_Java_Spring_Maven - Fatal编程技术网

Java Maven exec找不到Spring XML文件

Java Maven exec找不到Spring XML文件,java,spring,maven,Java,Spring,Maven,小结:我有一个非常基本的Java1.8和Spring4.0.5项目,我正在使用Maven 3.2.3来管理它。Maven exec似乎看不到我的Spring上下文,即使在我看来它在类路径中 我的spring上下文是 <project root>/src/main/resources/spring/ch2-beans.xml 在“mvn干净编译”之后,我明白了 我看到以下调试输出,这使我认为类路径确实正确: [DEBUG] Collected project classpath [/

小结:我有一个非常基本的Java1.8和Spring4.0.5项目,我正在使用Maven 3.2.3来管理它。Maven exec似乎看不到我的Spring上下文,即使在我看来它在类路径中

我的spring上下文是

<project root>/src/main/resources/spring/ch2-beans.xml
在“mvn干净编译”之后,我明白了

我看到以下调试输出,这使我认为类路径确实正确:

[DEBUG] Collected project classpath [/Users/me/Documents/workspace/BeginningSpring/chapter2/spring-book-ch2/target/classes]
[DEBUG] Adding to classpath : file:/Users/me/Documents/workspace/BeginningSpring/chapter2/spring-book-ch2/target/classes/
但我得到了以下错误:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/ch2-beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/ch2-beans.xml] cannot be opened because it does not exist
有人能告诉我解决这个问题的方向吗?我在上面发布的内容是解决这个问题的许多不同尝试的结果,当文件路径显然应该在类路径中时,我被错误难住了,所以这可能是自我上次编码(不久前…)以来发生的变化。谢谢

更新。。
考虑到exec:java可能存在类路径问题,我在一个单元测试中运行了相同的代码,在maven中执行。同样的错误。我还尝试将xml文件加载为“classpath:/spring/ch2beans.xml”,但没有修复它

我试图加载的Spring配置的文件名中有一个连字符,而类加载器似乎不接受这一点。在Java中,连字符在标识符名称中是非法的,显然这也适用于外部资源

一旦我从xml文件名中删除了连字符,它就被找到了


另一方面,我对使用这些连字符名称的“开始的春天”图书示例非常恼火。

我试图加载的春天配置的文件名中有一个连字符,而类加载器似乎不会接受。在Java中,连字符在标识符名称中是非法的,显然这也适用于外部资源

一旦我从xml文件名中删除了连字符,它就被找到了


另一方面,我对使用这些连字符名称的《初春》图书示例感到非常恼火。

您是否尝试使用新的ClassPathXmlApplicationContext(“Spring/ch2 beans.xml”);而不是新的ClassPathXmlApplicationContext(“/spring/ch2beans.xml”);?是的,我试过使用正斜杠,也试过不使用正斜杠,这两种方法都不起作用。不幸的是,如果您尝试使用新的ClassPathXmlApplicationContext(“spring/ch2 beans.xml”);而不是新的ClassPathXmlApplicationContext(“/spring/ch2beans.xml”);?是的,我试过使用和不使用正斜杠,不幸的是两者都不起作用
mvn -X exec:java -Dexec.mainClass="com.wiley.beginning.spring.ch2.Main"
[DEBUG] Collected project classpath [/Users/me/Documents/workspace/BeginningSpring/chapter2/spring-book-ch2/target/classes]
[DEBUG] Adding to classpath : file:/Users/me/Documents/workspace/BeginningSpring/chapter2/spring-book-ch2/target/classes/
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/ch2-beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/ch2-beans.xml] cannot be opened because it does not exist