Monday, July 11, 2011

surprise with spring while doing dependency injection to a child object

My child class (ChildDaoImpl) extends a parent class (ParentDaoImpl) and they are both managed by Spring. When I try to call a parent functionality from the child class with super.parentsMethod() I get NullPointerException.
I debug and see that the parent's injections are not complete at all. The DataSource field that should be injected in ParentDaoImpl is null. But the injection definitions seem ok in application context so where's the problem? I call the parent's bean separately and it works!

Spring does not trace the whole object hierarchy from the child class to the first ancestor which means that it does not do the necessary dependency injections to the parent chain. Thus my DataSource field in the ParentDaoImpl is not injected.
If you extend some class in your java code and you want the child class to work properly with parents' functionality you have to tell Spring explicitly that your child class has a parent.

So your application context should go from





to



No comments:

Post a Comment