0

I am using SpringSource suit 2.9.2 and a newbie in Spring MVC world. I am trying to get an hold over object of Application Context using the following code: -

ApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml");
UsersDaoImpl usersDao = context.getBean("usersDaoImpl", UsersDaoImpl.class);
Users user = usersDao.getUsers(name);
return user;

And its returning me FileNotFoundException exception. I tried different paths as well for ex. spring/root-context.xml etc. but nothing seems to work. Here is a stack trace: -

java.io.FileNotFoundException: class path resource [root-context.xml] cannot be opened because it does not exist
    org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    com.varundroid.demos.HomeController.testingDatabase(HomeController.java:65)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

enter image description here

Any help would be really appreciated.

Thanks.

4
  • 1
    where is your root-context.xml ?? ApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml"); - for this statement to work, you need to have root-context.xml in classpath i.e. src Commented Jul 4, 2012 at 7:56
  • It is created by SpringSource Suite at src/main/webapp/WEB-INF/spring/root-context.xml. I tried to create a new definition file as well called spring.xml which resides parallel to src folder but i am still getting the same Exception. Commented Jul 4, 2012 at 8:00
  • Not parallel. It must be src/spring.xml Commented Jul 4, 2012 at 8:01
  • Tried that as well. I uploaded an image of my file structure, take a look at it, may be you'll find what i am doing wrong here. Commented Jul 4, 2012 at 8:06

2 Answers 2

2

Chances are Spring cant find it in your class path.

Put that file under /src/main/resources (as shown in your image).
PS : That's my general practice and it has never failed till now.
Refer the image, here my context file is application-web-context.xml

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

The main question here is why do you need to create new application context in web application instead of using the existing ones.

As far as I can see from your setup, you should have ContextLoaderListener and DispatcherServlet declared in your web.xml. If so, you need to access context loaded by these classes rather than to create a new one.

If you want to access ApplicationContext inside a Spring bean, you can use ApplicationContextAware or autowiring. If you want to do it outside of Spring bean (for example, in Servlet or Filter), you can use WebApplicationContextUtils.

UPDATE:

You don't need to access ApplicationContext in this case. If you want to inject one Spring bean (DAO) into another one (controller), you can use autowiring:

@Autowired
private UserDao userDao;

Also note that if UserDaoImpl is a class that implements UserDao interface it would be better to use interface when declaring dependency.

4 Comments

Thank you for highlighting the real issue. I have updated my code. Can you please take a look and tell me is there any alternative to the procedure of getting an hold over instance of DaoImpl using ApplicationContextAware?? And i am using Autowired in my UsersDaoImpl.java to get data source from root-context.xml.
Where do you want to get UsersDaoImpl? Anyway, you shouldn't create new context, you should use the existing one.
In my HomeController class. I am new to Spring framework and using a default file structure provided by SpringSource suite. Can you tell me how to get an hold of existing ApplicationObject?
i am having a strange issue can you please take a look. stackoverflow.com/questions/11360273/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.