Spring的默认行为lazy-init [英] Spring default behavior for lazy-init

查看:78
本文介绍了Spring的默认行为lazy-init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的初学者,ESP控制反转.我很困惑,无法理解以下

I am beginner to spring, ESP Inversion of control. I was puzzled understanding the difference between the following

 <bean id="demo" class="Demo" lazy-init="false"/>
 <bean id="demo" class="Demo" lazy-init="true"/>
 <bean id="demo" class="Demo" lazy-init="default"/>

据我了解:lazy-init = false在启动时创建Bean,而lazy-init = true不在启动时创建Bean,而是根据对特定Bean的请求创建该Bean. 如果我的解释错误,请在这里纠正我.

To my understanding : lazy-init=false creates the bean at the startup and lazy-init=true doesn't create a bean at the startup rather creates the bean upon request for a particular bean. Correct me here, If my interpretation is wrong.

lazy-init的默认行为到底是什么?它将如何实例化?

what exactly the default behavior of lazy-init is? How would it instantiate?

推荐答案

默认行为为false:

The default behaviour is false:

默认情况下,ApplicationContext实现会急于创建和 在初始化过程中配置所有单例bean. 通常,这种预实例化是可取的,因为 立即发现配置或周​​围环境, 而不是几个小时甚至几天之后.当这种行为不是 理想的是,您可以通过以下方式阻止对单例豆的预实例化: 将Bean定义标记为延迟初始化.延迟初始化 bean告诉IoC容器何时创建一个bean实例 首先请求,而不是在启动时.

By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.

我建议阅读

这篇关于Spring的默认行为lazy-init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆