我们较常使用的是singleton bean依赖singleton bean,那出现singleton bean依赖prototype bean的时候应该怎么处理呢?有两种方式:
一,将被依赖的bean申明为prototype,然后通过ApplicationContext.getBean(xxx)的方式获取。我们知道对于非懒加载单例bean会在spring启动的时候被创建且只创建一次,其它的都会在显示getBean()的时候创建,又因为是prototype所以每次创建的bean都是新的。
二,通过lookup-method或@LookUp,使用的方式可以参考https://blog.csdn.net/qq_3025...

补充两点:
1,如果没特殊指明,创建的bean都是singleton,像项目里的@Controller/@Service
2,spring会负责singleton bean完整生命周期(从创建到销毁);对于prototype bean,spring只负责创建,何时销毁交给使用者定。

参考文章:
Spring lookup-method
spring中@Lookup的作用
Spring的replace-method标签


步履不停
38 声望14 粉丝

好走的都是下坡路