1.4.4. Lazy-initialized Beans(延迟初始化Bean)

 Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.4. Dependencies(依赖)

1.4.1. Dependency Injection(依赖注入)

1.4.1.1. Constructor-based Dependency Injection(基于构造函数的依赖注入) (Part I)

1.4.1.1. Constructor-based Dependency Injection(基于构造函数的依赖注入) (Part II)

1.4.1.2.Setter-based Dependency Injection(基于Setter的依赖注入)

1.4.1.3.Dependency Resolution Process(依赖解析过程)  

1.4.1.4. Examples of Dependency Injection(依赖注入示例)

1.4.2. Dependencies and Configuration in Detail(依赖与配置详细介绍)

1.4.3. Using depends-on(使用depends-on)

1.4.4. Lazy-initialized Beans(延迟初始化Bean)

1.4.5. Autowiring Collaborators(自动装配协作者)

1.4.6. Method Injection(方法注入)

1.4.6.1. Lookup Method Injection(查找方法注入)

1.4.6.2. Arbitrary Method Replacement(任意方法替换)

1.5. Bean Scopes(Bean作用域)


下载此文档精编完整版

 No.内容下载地址文档内容目录
1中英双语精编版 第一部分PDF下载内容目录
2中英双语精编版 第二部分PDF下载内容目录
3中文精编版 第一部分PDF下载内容目录
4中文精编版 第二部分PDF下载内容目录

更多章节内容,请点击查看:  Core Technologies


1.4.4. Lazy-initialized Beans(延迟初始化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 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:

默认情况下,ApplicationContext 实现在初始化过程中急切地去创建和配置所有单例(singleton bean。通常,这种预实例化(pre-instantiation)是可取的,因为配置或周围环境(configuration or surrounding environment)中的错误会立即被发现,而不是数小时甚至数天之后才被发现。当这种行为不可取时,可以通过将bean定义标记为延迟初始化(lazy-initialized)来防止单例(singleton) bean的预实例化。延迟初始化(lazy-initializedbean告诉IoC容器在第一次请求时而不是在启动时创建bean实例。

XML中,此行为由<bean/>元素上的lazy-init属性控制,如下例所示:

<bean id="lazy" class="com.something.ExpensiveToCreateBean" lazy-init="true"/>
<bean name="not.lazy" class="com.something.AnotherBean"/>

When the preceding configuration is consumed by an ApplicationContext, the lazy bean is not eagerly pre-instantiated when the ApplicationContext starts, whereas the not.lazy bean is eagerly pre-instantiated.

当前述配置被ApplicationContext使用时,ApplicationContext启动时,bean lazy 并不急于预实例化,而bean not.lazy则急于预实例化。

但是,当延迟初始化bean是未延迟初始化单例bean的依赖项时,

However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext creates the lazy-initialized bean at startup, because it must satisfy the singleton’s dependencies. The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.

You can also control lazy-initialization at the container level by using the default-lazy-init attribute on the <beans/> element, as the following example shows:

ApplicationContext会在启动时创建延迟初始化bean,因为它必须满足单例的依赖项(singleton’s dependencies)。延迟初始化bean被注入到非延迟初始化的其他地方的单例bean中。

您还可以通过在<beans/>元素上使用default-lazy-init属性来控制容器级别的延迟初始化(lazy-initialization),如下例所示:

<beans default-lazy-init="true">
    <!-- no beans will be pre-instantiated... -->
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月满闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值