This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Spring and the fly likes Spring primer: application context won't initialize my beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring primer: application context won Watch "Spring primer: application context won New topic
Author

Spring primer: application context won't initialize my beans

Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
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.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Try to debug setNameDao.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
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

Joined: Apr 12, 2006
Posts: 178
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

Joined: Jul 05, 2005
Posts: 1936
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring primer: application context won't initialize my beans
 
Similar Threads
Session creation falied - error
NullPointerException at the SessionFactory create line.
Controlling Hibernate Session with Spring
Spring 2.5 - Rolling back first method, when the second method throws Runtime ex
Hibernate: persist an object containing a list with more objects