# 2.4.1　创建 XML 配置规范

在使用 XML 为 Spring 装配 bean 之前，你需要创建一个新的配置规范。在使用 JavaConfig 的时候，这意味着要创建一个带有 @Configuration 注解的类，而在 XML 配置中，这意味着要创建 一个 XML 文件，并且要以元素为根。

最为简单的 Spring XML 配置如下所示：

```markup
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context" >
  
  <!-- configuration details go here />

</beans>
```

很容易就能看出来，这个基本的 XML 配置已经比同等功能的 JavaConfig 类复杂得多了。作为起步，在 JavaConfig 中所需要的只是 @Configuration，但在使用 XML 时，需要在配置文件的顶部声明多个 XML 模式（XSD）文件，这些文件定义了配置 Spring 的 XML 元素。

> 借助 Spring Tool Suite 创建 XML 配置文件创建和管理 Spring XML 配置文件的一种简便方式是使用 [Spring Tool Suite](https://spring.io/tools/sts)。在 Spring Tool Suite 的菜单中， 选择 File>New>Spring Bean Configuration File，能够创建 Spring XML 配置文件，并且可以选择可用的配置命名空间。

用来装配 bean 的最基本的 XML 元素包含在 spring-beans 模式之中，在上面这个 XML 文件中，它被定义为根命名空间。是该模式中的一个元素，它是所有 Spring 配置文件的根元素。

在 XML 中配置 Spring 时，还有一些其他的模式。尽管在本书中，我更加关注自动化以及基于 Java 的配置，但是在本书讲解的过程中，当出现其他模式的时候，我至少会提醒你。

就这样，我们已经有了一个合法的 Spring XML 配置。不过，它也是一个没有任何用处的配置，因为它（还）没有声明任何 bean。为了给予它生命力，让我们重新创建一下 CD 样例，只不过我们这次使用 XML  配置，而不是使用 JavaConfig 和自动化装配。


---

# 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/2.4-tong-guo-xml-zhuang-pei-bean/2.4.1-chuang-jian-xml-pei-zhi-gui-fan.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.
