Thursday, 18 December 2014

Spring Applicationcontext issue / IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist


Location of Application  Context XML file



Web.xml Entries


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>

        classpath:**/application-context.xml
    </param-value> 
</context-param>
<listener>
     <listener-class>

     org.springframework.web.context.ContextLoaderListener
     </listener-class>
</listener>

Bean entry in Application-Context.xml File.


<bean id="tweeterService" class="com.serviceImpl.TweeterServiceImpl">
</bean>


Getting beans instance from Application Context in java


@RequestMapping( value = "/pageInfo", method = RequestMethod.GET)
    public @ResponseBody String getPageInfo() {


/* 
*Try to avoid using this.
*
 ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml"); */


// Use below code for bean creation 
ApplicationContext ctx = new ClassPathXmlApplicationContext("../application-context.xml");

if(ctx!=null)
{   
 TweeterService ts =(TweeterService)ctx.getBean("tweeterService");

}  



Thanks 
Khansdroid