• 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

Spring primer: application context won't initialize my beans

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a context:



and two classes:





The context is loaded by ContextLoaderServlet defined in web.xml. When running the code I get NullPointerExceptions in NameServiceImpl because getNameDao() return null, so apparantly the nameDao property hasn't been initialized properly. Since my example is rather simple it can't be a big problem, I can't find it, though. Any hint is appreciated.
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to debug setNameDao.
 
Mike Himstead
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good suggestion, should have thought of that by myself. I debbuged the setter, it is called two times. First time with an object, the seccond time null is passed in, thus explaining why nameDao is null at runtime. But why is it called two times anyway?
 
Mike Himstead
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget my previous post, I guess I was bitten by a hot code replacement problem while deploying / debugging Glassfish. The basic problem still remains: the setters are definately called with a valid object and the dependency is properly set, I can see this using debug mode. But when using the class in question the dependency is null giving a NPE. Since I extended DI setting action dependencies as well I get the NPE earlier now in the action.

My web.xml looks like this:



My context looks like this by now:



The action:


Could this be an initialization sequence issue? What I have in mind is that Spring sets the dependencies but the objects are garbage collected afterwards due to a lack of references.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you're using Struts Actions. In that case, you have to integrate Struts and Spring. It'll not work automatically.

You can read Spring Reference Documentation.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic