天天看點

1.4.4. Lazy-initialized Beans(延遲初始化Bean) Spring Framework Documentation (5.3.10)

 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.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.5. Bean Scopes(Bean作用域)

關于Spring Framework Documentation (5.3.10)  核心技術的更多内容,請點選:

  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-initialized)bean告訴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>