Spring DI and OOP inheritance:

Hi all, I have a problem with Spring dependency injection. The scenario is as follows: I have a number of Data Access Objects (DAOs), Service Classes, and Web Layer Controllers. Data exchange between the different layers takes place via Sping DI. Injecting a DAO directly to my Controllers works fine but this brings in design issues. So I have included the Service layer that undertakes additional business logic before sending the data to the DAOs. The implementation of the service layer includes a base service class inherited by other classes: i.e: public class ServiceImpl implements Service{ (1)protected Dao<Customer, String> customerDao private void setCustomerDao(Dao<Customer, String> customerDao){ this.customerDao = customerDao; } ............ ............. ...... } public class CustomerServiceImpl extends ServiceImpl implements CustomerService{ private void getCustomer(String customerId){ (2) customerDao.getCustomerById(customerId); } ......................... .......................... ........................ } The customerDao in ServiceImpl injected by Spring as follows <bean id="serviceImpl" class="ke.co.x.x.ServiceImpl"> <property name="customerDao" ref="customerDao"/> </bean> <bean id="customerServiceImpl" class="ke.co.x.x.CustomerServiceImpl"> </bean> Every other dependency is taken care of in Spring's applicationContext.xml config file in a similar manner. When tested using JUnit or on the browser, a NullPointerException is thrown at (2). The DAO defined in the superclass is null in the subclass. This raises the question whether Spring's DI supports normal Java class inheritance and if it does, is there any special config required for this?

On Sun, May 24, 2009 at 3:17 PM, Duggan Kim <mdkimani@gmail.com> wrote:
Every other dependency is taken care of in Spring's applicationContext.xml config file in a similar manner.
When tested using JUnit or on the browser, a NullPointerException is thrown at (2). The DAO defined in the superclass is null in the subclass. This raises the question whether Spring's DI supports normal Java class inheritance and if it does, is there any special config required for this?
I am not so familiar with spring as such... but dependency injection frameworks require interfaces for both implementing classes *and* properties .... is the customerDAO object based on an interface ? ashok

Hi, did you test if setCustomerDAO() actually sets your customerDao first before trying to get it? On Mon, May 25, 2009 at 1:52 AM, Ashok Hariharan <ashok@parliaments.info>wrote:
On Sun, May 24, 2009 at 3:17 PM, Duggan Kim <mdkimani@gmail.com> wrote:
Every other dependency is taken care of in Spring's applicationContext.xml config file in a similar manner.
When tested using JUnit or on the browser, a NullPointerException is thrown at (2). The DAO defined in the superclass is null in the subclass. This raises the question whether Spring's DI supports normal Java class inheritance and if it does, is there any special config required for this?
I am not so familiar with spring as such... but dependency injection frameworks require interfaces for both implementing classes *and* properties .... is the customerDAO object based on an interface ?
ashok _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Skunkworks announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science - http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi - http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general

Found Solution: The problem with my code is that the subclasses did not have a constructor making a call to super(). Turns out this was necessary to init the superclass, although it is usually left out in spring applications: public subclass(){ super(); } On Mon, May 25, 2009 at 10:02 AM, Frankline Chitwa <frank.chitwa@gmail.com>wrote:
Hi, did you test if setCustoHi gmerDAO() actually sets your customerDao first before trying to get it?
On Mon, May 25, 2009 at 1:52 AM, Ashok Hariharan <ashok@parliaments.info>wrote:
On Sun, May 24, 2009 at 3:17 PM, Duggan Kim <mdkimani@gmail.com> wrote:
Every other dependency is taken care of in Spring's applicationContext.xml config file in a similar manner.
When tested using JUnit or on the browser, a NullPointerException is thrown at (2). The DAO defined in the superclass is null in the subclass. This raises the question whether Spring's DI supports normal Java class inheritance and if it does, is there any special config required for this?
I am not so familiar with spring as such... but dependency injection frameworks require interfaces for both implementing classes *and* properties .... is the customerDAO object based on an interface ?
ashok _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Skunkworks announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science - http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi - http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Skunkworks announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science - http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi - http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
participants (3)
-
Ashok Hariharan
-
Duggan Kim
-
Frankline Chitwa