Using depends-on
如果一个 Bean 是另一个 Bean 的依赖项,则通常表示一个 Bean 被设置为另一个 Bean 的属性。通常,您可以使用基于 XML 的配置元数据中的 <<`<ref/>`元素,beans-ref-element>> 来实现此目的。然而,有时 Bean 之间的依赖关系不那么直接。一个示例是当需要触发类中的静态初始化程序时,例如,数据库驱动程序注册。depends-on
属性可以显式地强制一个或多个 Bean 在使用此元素初始化 Bean 之前进行初始化。以下示例使用 depends-on
属性来表示对单个 Bean 的依赖:
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 <<`<ref/>`
element,beans-ref-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 id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />
要表示对多个 Bean 的依赖,请提供一个 Bean 名称列表作为 depends-on
属性的值(逗号、空格和分号是有效的定界符):
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 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 |