What is the best aproach to create a non-static inner class in Spring?
class A {
public class B {}
B b;
public void setB(B b) {this.b = b;}
}
this seems to work, but i want to avoid the need for a constructor argument:
<bean id="a" class="A">
<property name="b">
<bean id="b" class="A$B">
<constructor-arg ref="a"/>
</bean>
</property>
</bean>
At some point, you need to specify the outer object, there’s no avoiding that. You could, however, move this into the Java, and out of the XML, by adding a factory method to
Athat creates the innerB:And then you can do:
So the XML is simpler, but the java is more complex. Spring is smart enough not to get upset about apparent circular references.
Take your pick, you need to do one or t’other.