How to get path in spring to access configuration file
ClassPathResource
is used to refer the path of the resource in the classpath. The path
argument passed in the ClassPathResource
refers to the absolute path within the class path.So, use
Resource resource=new ClassPathResource("com/rajeev/spring/DAOImpl/SelectCfg.xml");
Try to use FileSystemResource
to mention absolute path in the filesystem
rajeev pani..
In a day, when you don't come across any problems - you can be sure that you are travelling in a wrong path.
Updated on June 04, 2022Comments
-
rajeev pani.. almost 2 years
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 errorE:\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.