最近在项目开发过程中碰到了singleton bean中依赖prototype bean的问题,在官网和社区学习了一下,发现有一些使用方法是错误的,在这里自己测试了几个正确的使用方式,在这里供大家交流。
首先是示例代码,这是省略版的应用场景。
@Service(Scope="singleton")
public class SingletonServiceImpl implements SingletonService{
@Resource
PrototypeBean prototypeBean;//直接在单例模式中注入原型bean,prototypeBean其实只实例化了一次,原型模式的作用完全没有发挥
public void start(){
prototypeBean.setFoo(new Foo());
prototypeBean.setBar(new Bar());
process(prototypeBean);
}
public void process(PrototypeBean prototypeBean){
//......
}
}
@Service(Scope="prototype")
public class PrototypeBean{
private int count = 0;
private Foo foo;
private Bar bar;
public