I have the following class structure
public class Outer{
private Mapper a;
....
private class MapperA implements Mapper {
}
private class MapperB implements Mapper {
}
}
In my Spring config file I would like to create a an Outer bean, and assign one of MapperA or MapperB as a property. Is this possible?
<bean id="outer" class="mypackage.Outer">
<property name="a" ?????='????' />
</bean>
Edit: Some more info, based on the feedback from answers:
-
I got lazy with my above example. I do have a public setter/getter for the Mapper instance variable.
-
The reason all of the Mapper classes are inner classes is because there could potentially be many of them, and they will only ever be used in this class. I just don’t want a ton of cruft classes in my project. Maybe a factory method is a better idea.
Normally you’d need a setter for the
MapperwithinOuter, and an instance of the requiredMapper. But as these are:classes, that becomes a bit tricky (as you’ve identified). If you make them public, I’m sure you could creae an instance using
Outer$MapperAetc. But that seems a little nasty. So:MapperAorMapperB. i.e. there’s some factory capability here.The simplest thing to do is to really determine if they need to be inner/private. If so, then they really shouldn’t be referenced within the config, which should be talking about publicly accessible classes.