Spring FileSystemXmlApplicationContext don't find the bean configuration file and go into error

折月煮酒 提交于 2020-01-11 07:16:28

问题


I am trying to do the following Spring "Hello World" example that use the FileSystemXmlApplicationContext as implementation of ApplicationContext interface.

This implementation have to take the full path of the XML bean configuration as constructor parameter, something like the previous example:

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

I use Linux and my Beans configuration file is under the following path: /home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml, so in my code I have:

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

The problem is that when I try to run my application, in the STS\Eclipse console I have the following error message (seems that don't find the file):

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

Why? How can I solve?

Tnx

Andrea


回答1:


NOTE: Plain paths will always be interpreted as relative to the current VM working directory, even if they start with a slash. (This is consistent with the semantics in a Servlet container.) Use an explicit "file:" prefix to enforce an absolute file path.

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



来源:https://stackoverflow.com/questions/14483160/spring-filesystemxmlapplicationcontext-dont-find-the-bean-configuration-file-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!