# 2.2　自动化装配 bean

在本章稍后的内容中，你会看到如何借助 Java 和 XML 来进行 Spring 装配。尽管你会发现这些显式装配技术非常有用，但是在便利性方面，最强大的还是 Spring 的自动化配置。如果 Spring 能够进行自动化装配的话，那何苦还要显式地将这些 bean 装配在一起呢？

Spring 从两个角度来实现自动化装配：

* 组件扫描（component scanning）：Spring 会自动发现应用上下文中所创建的 bean。
* 自动装配（autowiring）：Spring 自动满足 bean 之间的依赖。

组件扫描和自动装配组合在一起就能发挥出强大的威力，它们能够将你的显式配置降低到最少。

为了阐述组件扫描和装配，我们需要创建几个 bean，它们代表了一个音响系统中的组件。首先，要创建 CompactDisc 类，Spring 会发现它并将其创建为一个 bean。然后，会创建一个 CDPlayer 类，让 Spring 发现它，并将 CompactDiscbean 注入进来。


---

# 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://potoyang.gitbook.io/spring-in-action-v4/di-2-zhang-zhuang-pei-bean/untitled.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.
