Results 1 to 3 of 3

Thread: using DI with non-static inner classes

  1. #1

    Default using DI with non-static inner classes

    Apologies in advance for posting what seems like a trivial question (I'm new to Spring), but I've scoured the web and can't find an answer.

    I'd like to use Dependency Injection to initialize an outer bean with an instance of a non-static inner bean. Spring is giving me a NoSuchMethodException when the container loads, which I understand: it tries to instantiate the inner bean (declared as an anonymous bean inside a <property name="myInnerClassInstance">), but can't do so because the outer class (I suppose) doesn't exist yet.

    Now, if I make the inner class static, and just give it an instance of the outer bean via DI, then I'm golden. That's what I'm doing for a workaround. That sort of defeats the purpose of the inner class, though ... I'd almost rather just bag DI for the inner class and instantiate it inside the zero-arg constructor of the outer class, rather than muck up the design.

    It seems like somebody must've solved this. Is there some kind of lazy-init or factory-method magic that lets Spring get at at non-static inner class within the outer bean's definition?

    Failing that, does anybody have a better pattern for inner class DI? I'm trying to do things the "Spring way," keeping messy config stuff in my code, but this one's got me stumped.

    Thanks-
    Rogers

  2. #2
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    When I use inner classes I tend to initialized them either in a constructor, or in the init-method. If I need to set properties on an inner class, I would make it part of initializing the outer class.

    I usually only create inner classes in two cases:
    - Encapsulating some functionality only local to that class (ie RowMapper, utility classes, etc)
    - A local object that implements a particular public interface that gets passed back as the result of some method.
    Bill

  3. #3

    Default

    Well, it looks like that's what I'm stuck with- using DI to set outer class properties, then initializing the inner class in a constructor or init-method. that seems messier than it out to be ...

    class Outer{
    String initString1;
    String initString2;
    String initString3;
    Inner myInnerInstance;

    outer(){
    myInnerInstance = new Inner();
    myInnerInstance.myInitString1 = initString1;
    myInnerInstance.myInitString2 = initString2;
    myInnerInstance.myInitString3 = initString3;
    }

    //getters/setters for all three initStrings in Outer ...

    class Inner{
    String myInitString1;
    String myInitString2;
    String myInitString3;
    }
    }

    Now I've defined properties on the outer class that're doubles of the inner class properties, just for the sake of injecting them with DI.

    The use case here is a DAO that extends HibernateTemplate, performing various CRUD operations; and contains an inner class that extends a WebSpider class I've written. The inner class needs to use the CRUD functions of the outer class to save information it builds from the spidered page source, so it can't be static. (it could ... it would just need an instance of the outer class at some point to do its thing, which is what I do now for a workaround) The inner MyWebSpider class takes a number of configuration variables which seem tailer-made for Spring DI, but I hate to muck up the DAO a la the above example, and as you can see, making MyWebSpider static is artificial- it depends on the DAO for its work, doesn't make sense as an independant entity. Of the two, I think the latter is the less intrusive workaround, but I'd still prefer to keep the design "right" (i.e. what should be done irrespective of Spring) if at all possible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •