I am trying to inject a prototype
bean in a singleton
bean such that every new call to a singleton bean method has a new instance of the prototype bean.
Consider a singleton bean as below:
@Component
public class SingletonBean {
@Autowired
private PrototypeBean prototypeBean;
public void doSomething() {
prototypeBean.setX(1);
prototypeBean.display();
}
}
I expect that every time the doSomething()
method is called, a new PrototypeBean
instance is utilized.
Below is the prototype bean:
@Component
@Scope(value="prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class PrototypeBean {
Integer x;
void setX(Integer x) {
this.x = x;
}
void display() {
System.out.println(x);
}
}
What seems to be happening is that spring is being overeager in handing over a new instance of PrototypeBean in the doSomething()
method. That is, the 2 lines of code in the doSomething()
method are creating a new instance of prototypeBean in each line.
And so in the 2nd line - prototypeBean.display()
prints NULL.
What is missing in configuration for this injection?