Spring Context File Not Found
While working on Spring framework you will have to load context file first to load all bean and framework definition. Sometime while working on Eclise after setting up everything correctly Eclipse will still complain about file not found exception and you will exception similiar as below:
INFO: Loading XML bean definitions from class path resource [JavaHonk-context.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [JavaHonk-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [JavaHonk-context.xml] cannot be opened because it does not exist 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.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.javahonk.TestApp.main(TestApp.java:13) Caused by: java.io.FileNotFoundException: class path resource [JavaHonk-context.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ... 13 more
Sample project with cofiguration and resource file structure:
- In this case If you have project structure already set up and you already put context file in classpath or resources folder then right click project –> Properties –> Java Build Path –> Click Source tab then remove both resources and config folder and add back again. This will referesh classpath again and you those classpath issue thould be resolved.
- Sample Java class to load context file:
import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "marketdataservice-context.xml"); ((ConfigurableApplicationContext) context).close(); } }
- For more information please visit Spring tutorial here