关于spring:关于spring里的singleton和prototype

咱们较常应用的是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标签

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理