我刚到 Spring 。 我尝试编写简单的项目和堆栈,但有奇怪的异常 FileNotFoundException 。
在我看来,加载app-context.xml
存在一些问题。但我确信设置了正确的绝对路径。
有什么建议吗?
主要代码:
包com.prospring.ch2;
public class HelloWorldSpringDI {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml");
MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
mr.render();
}
}
此处内容app-context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="provider" class="com.prospring.ch2.HelloWorldMessageProvider" />
<bean id="rendere" class="com.prospring.ch2.StandartOutMessageRenderer"
p:messageProvider-ref="provider" />
<!--<description>Example configuration to get you started.</description -->
<!-- context:component-scan base-package="com.prospring.ch2" /> -->
</beans>
控制台消息的片段:
14:24:06,360 INFO t.support.ClassPathXmlApplicationContext: 456 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3f62e847: startup date [Wed Nov 13 14:24:06 EET 2013]; root of context hierarchy
14:24:06,509 INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean definitions from class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml] cannot be opened because it does not exist
我检查了 app-context.xml
从终端的路径,一切看起来都正常:
nazar_art@nazar-desktop:~/workspace/ch2/src/main/resources/META-INF/spring$ pwd
/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring
nazar_art@nazar-desktop:~/workspace/ch2/src/main/resources/META-INF/spring$ ls -lg
total 4
-rw-rw-r-- 1 nazar_art 867 Nov 13 14:25 app-context.xml
这是我的结构:
- 如何解决这个问题?
最佳答案
您正在使用类路径应用程序上下文加载器 ClassPathXmlApplicationContext并向其传递绝对路径。
您需要使用FileSystemXmlApplicationContext
如果您想使用ClassPathXmlApplicationContext那么您必须确保您的 app-context.xml
位于应用程序的类路径中,并传递适当的资源名称,该名称与文件系统上的绝对路径不同。
关于java - spring 实用程序项目 - Ubuntu 上的 java.io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19954274/