问题:
我创建了一个程序,该程序将使用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
DirectoryStructure
最佳答案
如果资源在类路径中,则不应使用旨在指定绝对路径的FileSystemResource
。
也是ClassPathResource
实现的org.springframework.core.io.Resource
更合适。
在您的实际代码中,src
文件夹位于类路径中,因此应该没问题:
res = new ClassPathResource("com/jc/cfgs/applicationContext.xml");