gpt4 book ai didi

惰性初始化的 Spring 默认行为

转载 作者:IT老高 更新时间:2023-10-28 13:02:28 26 4
gpt4 key购买 nike

我是spring的初学者,ESP控制反转。我很困惑理解以下之间的区别

 <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。在这里纠正我,如果我的解释是错误的。

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

最佳答案

默认行为是假的:

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.

I suggest reading up

关于惰性初始化的 Spring 默认行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15092898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com