Creating lazily initialized Spring beans using annotation based configuration

若如初见. 提交于 2019-11-30 17:03:28

问题


I am using Spring's @Component annotation to configure many of the beans in my Spring 3.0 application. I would like to know if it's possible to construct some of these beans lazily - especially the prototype beans?


回答1:


To declare lazy-initialized bean you can use @Lazy annotation.

Note, however, that it doesn't make sense for prototype beans - they can't be eagerly initialized, so there is no need to mark them lazy.




回答2:


Lazy initialization isn't an option in the context of prototype-scoped beans. Those beans are instantiated and initialized on demand every time something asks for them, so they are, by nature, lazily initialized.



来源:https://stackoverflow.com/questions/2365677/creating-lazily-initialized-spring-beans-using-annotation-based-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!