对于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 being lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
In XML, this behavior is controlled by the lazy-init attribute on the <bean/> element, as the following example shows:
<b
Spring默认会立即创建并配置所有单例Bean,但通过`lazy-init`属性可实现延迟初始化,即在首次请求时才创建Bean。如果一个延迟初始化的Bean依赖于非延迟初始化的Bean,它仍会在启动时创建。`default-lazy-init`属性可全局设置Bean的延迟初始化。当`lazy-init`设为`true`,或`default-lazy-init`设为`true`时,Bean将在第一次使用时初始化。
最低0.47元/天 解锁文章
4321

被折叠的 条评论
为什么被折叠?



