问题:
我创建了一个程序,它将使用FilesystemResource从指定路径读取我的applicationContext.xml文件并调用B.Method。 我无法理解为什么我会收到这样的错误:
任何人都可以指导我如何解决这个问题:
以下是代码
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
答案 0 :(得分:0)
如果资源位于类路径中,则不应使用旨在指定绝对路径的FileSystemResource
。
ClassPathResource
也是org.springframework.core.io.Resource
实施更合适。
在您的实际代码中,src
文件夹位于类路径中,因此这应该没问题:
res = new ClassPathResource("com/jc/cfgs/applicationContext.xml");