0

我刚到春天。我试图用奇怪的异常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

这是我的打击:

春季项目罢工

  • 如何解决这个麻烦?
4

2 回答 2

4

您正在使用类路径应用程序上下文加载器ClassPathXmlApplicationContext并将绝对路径传递给它。

您需要使用FileSystemXmlApplicationContext

如果您想使用ClassPathXmlApplicationContext,那么您必须确保您app-context.xml在应用程序的类路径中并传递适当的资源名称,这与文件系统上的绝对路径不同。

于 2013-11-13T13:01:44.570 回答
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");
  ...
}

FileSystemXmlApplicationContext

ClassPathXmlApplicationContext

于 2013-11-13T13:08:17.470 回答