1

我正在尝试执行以下 Spring“Hello World”示例,该示例使用FileSystemXmlApplicationContext作为 ApplicationContext 接口的实现。

此实现必须将XML bean 配置的完整路径作为构造函数参数,类似于前面的示例:

ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");

我使用 Linux 并且我的 Beans 配置文件位于以下路径下: /home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml,所以在我的代码中我有:

ApplicationContext context = new FileSystemXmlApplicationContext("/home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml");

问题是,当我尝试运行我的应用程序时,在 STS\Eclipse 控制台中出现以下错误消息(似乎找不到该文件):

INFO: Loading XML bean definitions from file [/home/andrea/Documents/ws/myapplicationcontextexample/home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/home/andrea/Documents/ws/myapplicationcontextexample/home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml]; nested exception is java.io.FileNotFoundException: home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml (File o directory non esistente)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at org.andrea.myexample.myapplicationcontextexample.MainApp.main(MainApp.java:14)
Caused by: java.io.FileNotFoundException: home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml (File o directory non esistente)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:113)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 13 more

为什么?我该如何解决?

肿瘤坏死因子

安德烈亚

4

1 回答 1

8

注意:普通路径将始终被解释为相对于当前 VM 工作目录,即使它们以斜杠开头。(这与 Servlet 容器中的语义一致。)使用显式的“file:”前缀来强制使用绝对文件路径。

参考:http ://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/support/FileSystemXmlApplicationContext.html

于 2013-06-14T15:15:49.807 回答