• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem accessing application context file in controller class.

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am developing a small spring mvc app.
I have spring-context.xml file containing spring MVC's dispatcher servlet configuration.
root-context.xml as application context.

I am declaring my Datasource/daoimpl beans in root-context.xml file.
How do i access root-context.xml file in controller class and invoke getBeans() method on it to get the beans?
Below is lines of code from controller class.

This is one of the several ways I tried accessing it.
root-context.xml file is declared in web.xml file as context param.


Is there a way I can access this "contextConfigLocation" parameter in my controller class to access this xml file?

Complete path of all the files mentioned is :
/SpringMVCStudent/src/main/webapp/WEB-INF/spring/root-context.xml
/SpringMVCStudent/src/main/java/java9s/springmvcstudent/controller/StudentController1.java
/SpringMVCStudent/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
/SpringMVCStudent/src/main/webapp/WEB-INF/web.xml

Its a problem in one trivial line of code which took my entire day and still not solved.
Thanks in advance
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you don not instantiate new contexts in controller. The context has already configured by Spring, try this



hope that helps.
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can even use the @Autowired, and start using dao object. Spring takes care of setting the property.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surendra is correct. Don't implement BeanFactoryAware or have a reference to ApplicationContext at all. Do not call getBean(), this defeats the whole purpose of dependency injection. In your case you need a particular Spring bean, let Spring provide it (using @Autowired) there is no need to look it up from the context.
 
Antraa Sethi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thank you all for your responses. I am sure that is the solution to this problem I was having.

As I tried to make the changes and see for myself in eclipse, my project has become unstable.
Showing errors everywhere, in all files. in pom.xml it shows the following msg as error:
"Lifecycle mapping "org.eclipse.m2e.jdt.JarLifecycleMapping" is not available. To enable full functionality, install the lifecycle mapping and run Maven->Update Project
Configuration."

I dont know what happened. please advice.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like this is the issue of Maven and Eclipse, Please post your question on build tool forum.
 
Antraa Sethi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I uninstalled and reinstalled m2e and it got resolved jus now.
I implemented the changes you suggested and it is working now.

Thanks alot....
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic