我刚接触春天。 我尝试用奇怪的异常FileNotFoundException编写简单的项目和堆栈。
在我看来,加载app-context.xml
是一个问题。但我确定设置正确的绝对路径。
有什么建议吗?
主要代码:
package 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
以下是我的看法:
答案 0 :(得分:4)
您正在使用类路径应用程序上下文加载器ClassPathXmlApplicationContext并将绝对路径传递给它。
您需要使用FileSystemXmlApplicationContext
如果要使用ClassPathXmlApplicationContext,则必须确保app-context.xml
位于应用程序的类路径中并传递相应的资源名称,该名称与文件系统上的绝对路径不同
答案 1 :(得分:0)
使用
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml");
...
}
或
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"META-INF/spring/app-context.xml");
...
}