天天看点

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>