Using depends-on
If a bean is a dependency of another bean, that usually means that one bean is set as a property of another. Typically you accomplish this with the `` element in XML-based configuration metadata. However, sometimes dependencies between beans are less direct. An example is when a static initializer in a class needs to be triggered, such as for database driver registration. The depends-on
attribute can explicitly force one or more beans to be initialized before the bean using this element is initialized. The following example uses the depends-on
attribute to express a dependency on a single bean:
如果一个bean是另一个bean的依赖项,这通常意味着一个bean被设置为另一个bean的属性。通常使用基于xml的配置元数据中的<ref/>元素来完成此任务。然而,有时bean之间的依赖关系并不那么直接。例如,需要触发类中的静态初始化器时,例如数据库驱动程序注册时。depends-on
属性可以显式地强制在初始化使用此元素的bean之前初始化一个或多个bean。下面的示例使用depends-on属性来表示对单个对象的依赖:
<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />
To express a dependency on multiple beans, supply a list of bean names as the value of the depends-on
attribute (commas, whitespace, and semicolons are valid delimiters):
要表达对多个bean的依赖关系,请提供一个bean名称列表作为depends-on属性的值(逗号、空格和分号是有效的分隔符):
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean>
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
The
depends-on
attribute can specify both an initialization-time dependency and, in the case of singleton beans only, a corresponding destruction-time dependency. Dependent beans that define adepends-on
relationship with a given bean are destroyed first, prior to the given bean itself being destroyed. Thus,depends-on
can also control shutdown order.
depends-on
属性既可以指定初始化时的依赖项,也可以指定对应的销毁时依赖项(仅在单例bean中)。在销毁给定bean本身之前,首先销毁与给定bean定义依赖关系的依赖bean。因此,依赖还可以控制关机顺序。 |
Lazy-initialized Beans延迟初始化
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.
默认情况下,ApplicationContext
实现在初始化过程中急切地创建和配置所有单例bean
。一般来说,这种预实例化是可取的,因为配置或周围环境中的错误会立即被发现,而不是几个小时甚至几天后。当这种行为不可取时,可以通过将bean定义标记为延迟初始化来防止单例bean的预实例化。一个延迟初始化的bean告诉IoC容器在第一次被请求时而不是在启动时创建一个bean实例。
In XML, this behavior is controlled by the lazy-init
attribute on the <bean/>
element, as the following example shows:
在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
使用时,lazy
bean不会在ApplicationContext启动时预先实例化,而not.lazy
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.
但是,当一个延迟初始化的bean作为不是延迟初始化的singleton bean的依赖项时,ApplicationContext
会在启动时创建 lazy initialized bean,因为它必须满足singleton的依赖关系。惰性初始化的bean被注入到非惰性初始化的其他地方的单例bean中。
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:
还可以通过使用<beans/>元素上的 default-lazy-init
属性在容器级别
控制lazy初始化,如下例所示:
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
下一节:Autowiring Collaborators 自动装配合作者