# 1.4.3. 使用depends-on

如果一个 bean 是另一个 bean 的依赖项，那通常意味着一个 bean 被设置为另一个 bean 的属性。通常，您使用基于 XML 的配置元数据中的[`<ref/>` 元素来完成此操作。](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-ref-element)但是，有时 bean 之间的依赖关系不那么直接。例如，当需要触发类中的静态初始化程序时，例如用于数据库驱动程序注册。在初始化使用此元素的 bean 之前，`depends-on`属性可以显式强制初始化一个或多个 bean。以下示例使用`depends-on`属性来表达对单个 bean 的依赖：

```xml
<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />
```

要表达对多个 bean 的依赖关系，请提供 bean 名称列表作为`depends-on`属性值（逗号、空格和分号是有效的分隔符）：

```xml
<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" />
```

> `depends-on`属性既可以指定初始化时间依赖项，也可以指定对应的销毁时间依赖项（仅在[单例bean 的情况下）。](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes-singleton)定义与给定 bean 的关系的从属`depends-on` bean首先被销毁，然后给定 bean 本身被销毁。这样，`depends-on`也可以控制关机顺序。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-he-xin-gong-neng/1.ioc-rong-qi-he-bean-jian-jie/1.4.-yi-lai-xiang/1.4.3.-shi-yong-dependson.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
