第二章 IoC Bean的初始化与延迟加载
在默认情况下,Spring的ApplicationContext容器在启动的时候,会自动实例化所有singleton的Bean并缓存在容器中。虽然启动时会花费一些时间,但带来两个好处:
1、对Bean提前实例化操作会及早发现一些潜在的配置问题;
2、Bean以缓存的方式保存,当运行期使用到该Bean的时候无需再实例化,加快运行的效率。
如果用户不希望容器启动的时候提前实例化singleton的Bean,可以通过lazy-init属性进行控制:
<bean scope="singleton" lazy-init="true"></bean>
也可以在beans标签中使用default-lazy-init对所有的singleton的Bean进行延迟加载:
<beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-lazy-init="true">
被设置为延迟加载的Bean在某些情况下依旧会提前实例化:如果这个Bean被其它需要提前实例化的Bean引用到,那么Spring也将忽略延迟加载设置。
一般我们不做延迟加载。
最后更新:2017-04-03 18:52:13
上一篇:
android源码地址
下一篇:
C++内存管理学习笔记(5)
软件更新时候出现和原包名冲突
银泰首个新零售双11:CEO陈晓东“微顿悟”了
编程思维训练(一)
金牌导购系统开发平台搭建
iBATIS多表查询之N+1 Select
Apache HttpComponents Client 4.0快速入门/升级-1.GET方法访问网页
ARM免预付授权费用计划增加Cortex-M3架构设计 加速物联网发展
工作五年“攒”够100万,程序猿们,我可没开玩笑!8条建议抱走不谢
错误整理:No plugin found for prefix 'jetty' in the current project and in the plugin groups
The specified executable is not a validapplication for this OS platform.