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