Java 为什么不使用FileSystemResource类读取xml文件
问题: 我创建了一个程序,该程序将使用FilesystemResource从指定路径读取applicationContext.xml文件,并调用B.Method。 我无法理解为什么会出现这样的错误: 有谁能指导我如何解决这个问题: 下面是代码Java 为什么不使用FileSystemResource类读取xml文件,java,spring,Java,Spring,问题: 我创建了一个程序,该程序将使用FilesystemResource从指定路径读取applicationContext.xml文件,并调用B.Method。 我无法理解为什么会出现这样的错误: 有谁能指导我如何解决这个问题: 下面是代码 public static void main(String[] args) { Resource res=null; BeanFactory factory=null; Object obj=null; WishMessa
public static void main(String[] args) {
Resource res=null;
BeanFactory factory=null;
Object obj=null;
WishMessageGenerator generator=null;
//Locate Cfg File
res=new FileSystemResource("com/jc/cfgs/applicationContext.xml");
//Create IOC Container
factory=new XmlBeanFactory(res);
//getBean Class Object
obj=factory.getBean("wish");
//TypeCasting
generator=(WishMessageGenerator)obj;
//Invoke Businees Method
System.out.println("Message ="+generator.wishMsg("jalaj"));
}
}
错误显示:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/Users/mansi/Desktop/springapp/SpringApp4/com/jc/cfgs/applicationContext.xml]; nested exception is java.io.FileNotFoundException: com/jc/cfgs/applicationContext.xml (No such file or directory)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:79)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:67)
at com.jc.test.Test.main(Test.java:20)
Caused by: java.io.FileNotFoundException: com/jc/cfgs/applicationContext.xml (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:115)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 4 more
线程“main”org.springframework.beans.factory.BeanDefinitionStoreException:IOException解析文件[/Users/mansi/Desktop/springapp/SpringApp4/com/jc/cfgs/applicationContext.XML]中的XML文档;嵌套的异常是java.io.FileNotFoundException:com/jc/cfgs/applicationContext.xml(没有这样的文件或目录)
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
位于org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:79)
位于org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:67)
位于com.jc.test.test.main(test.java:20)
原因:java.io.FileNotFoundException:com/jc/cfgs/applicationContext.xml(无此类文件或目录)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:138)
位于org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:115)
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 4更多
如果资源位于类路径中,则不应使用设计用于指定绝对路径的
FileSystemResource
ClassPathResource
这也是一个org.springframework.core.io.Resource
实现更合适
在您的实际代码中,src
文件夹位于类路径中,因此这应该可以:
res = new ClassPathResource("com/jc/cfgs/applicationContext.xml");
如果我从类路径中删除applicationContext.xml,那么?请参阅上面的目录结构。我已完成编辑,为什么要删除它?您应该将根据运行环境而不是所有spring配置更改的信息外部化。我不想保留在类路径中。我想保留在指定的路径中您不应该保留,如果您想这样做,文件不应该在项目源代码中,而应该在外部,并且应该为FileSystemResource提供绝对路径