我正在尝试使用 FileSystemXmlApplicationContext 作为ApplicationContext接口的实现,执行以下Spring“Hello World”示例。
此实现必须采用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
为什么呢?我怎么解决?
TNX
安德烈
答案 0 :(得分:8)
注意:普通路径将始终被解释为相对于当前VM工作目录,即使它们以斜杠开头。 (这与Servlet容器中的语义一致。)使用显式“file:”前缀来强制执行绝对文件路径。