Jan 12th, 2006, 08:42 PM
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.
Jan 12th, 2006, 10:45 PM
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.
Jan 13th, 2006, 11:34 AM
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 ...
myInnerInstance = new Inner();
myInnerInstance.myInitString1 = initString1;
myInnerInstance.myInitString2 = initString2;
myInnerInstance.myInitString3 = initString3;
//getters/setters for all three initStrings in Outer ...
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.