I have written a Spring program where the following classes, interfaces and xml files are in different packages. I use Eclipse Kepler.
SelectClient.java
package com.rajeev.spring.action;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.rajeev.spring.DAOI.Select;
/**
* @author rajeev
*
*
*/
public class SelectClient {
/**
* @param args
*/
public static void main(String[] args) {
String path=System.getProperty("user.dir");
System.out.println(path+"/src/com/rajeev/spring/DAOImpl/SelectCfg.xml");
Resource resource=new ClassPathResource(path+"/src/com/rajeev/spring/DAOImpl/SelectCfg.xml");
XmlBeanFactory beanFactory=new XmlBeanFactory(resource);
Object object=beanFactory.getBean("sb");
Select select=(Select)object;
System.out.println("emp name is:"+select.fetchName(101));
}
}
The problem is that when I execute the SelectClient.java
, it is giving following error
E:\javahyd\eclipse\Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml Jan 17, 2014 11:41:43 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml]; nested exception is java.io.FileNotFoundException: class path resource [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml] cannot be opened because it does not exist 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.xml.XmlBeanFactory.(XmlBeanFactory.java:78) at org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:66) at com.rajeev.spring.action.SelectClient.main(SelectClient.java:26) Caused by: java.io.FileNotFoundException: class path resource [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 4 more
The above exception, is due to the path. When I use the same path in run(windows+r) the particular file it is opening. I dont want to keep my configuration file out of any packages.
Best Answer
ClassPathResource
is used to refer the path of the resource in the classpath. Thepath
argument passed in theClassPathResource
refers to the absolute path within the class path.So, useTry to use
FileSystemResource
to mention absolute path in the filesystem