For reference, if someone comes across this via Google:
I ended up needing to declare it in the spring.xml
. I tried @Lookup
, but even that didn't work due to the prototype-bean referencing yet another prototype-bean.
This is how it was recommended here,
but it does not work:
@Component("proto1")
@Scope("prototype")
class MyPrototypeBean1 {
@Lookup(value="proto2")
protected MyPrototypeBean2 createBean2() { return null; }
}
@Component("proto2")
@Scope("prototype")
class MyPrototypeBean2 {
}
@Component("singleton")
class MySingleton {
@Lookup(value="proto1")
protected MyPrototypeBean1 createBean1() { return null; }
}
This results in the error message "Cannot apply @Lookup to beans without corresponding bean definition" when trying to create "innerBean...".
I assume it is due to "lookup methods cannot get replaced on beans returned from factory methods where we can't dynamically provide a subclass for them" as is quoted in the link above.
So what I ended up doing in the spring.xml
:
<bean name="proto2" class="my.package.PrototypeBean2" />
<bean name="proto1" class="my.package.PrototypeBean1" >
<lookup-method name="createBean2" bean="proto2" />
</bean>
<bean name="singleton" class="my.package.SingletonBean" >
<lookup-method name="createBean1" bean="proto1" />
</bean>
And this works.
For the unit tests, I had to subclass the respective classes:
class SingletonUnitTest {
@Mock
MyPrototypeBean1 bean1;
@InjectMocks
DummySingleton sut;
@Before public void setBean1() {
sut.bean = bean1;
}
static class DummySingletonBean extends MySingeton {
MyPrototypeBean1 bean;
protected MyPrototypeBean1 createBean1() {
return bean;
}
}
}