Now my controller makes a call to the emailSender bean using the following code:
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
EmailSender sender =(EmailSender)context.getBean("emailSender");
The exception I get is: Error 404: Request processing failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: 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
I am loading the applicationContext.xml at serevr start-up still my code is not able to locate this file.
Basically it is saying that it is looking for that file in a particular location and it isn't located there.
Where is the file actually located?
Based on what you posted it is looking in the root of the classpath for that file.
Based on the name of the file I am assuming you are trying to use the naming convention, which includes putting the file in the WEB-INF directory. Note that that directory is not in the classpath in anyway. Typically for Spring MVC web applications, you use the ContextLoaderListener in the web.xml file to load an XmlWebApplicationContext, and that will automatically load in the WEB-INF/applicationContext.xml file and create an ApplicationContext that your Controllers loaded by the DispatcherServlet can have access to, so you can just DI the bean into your controller, via xml or @Autowired annotation.