# 10.2.4. 注册处理程序和模式

编码完成。剩下要做的就是让 Spring XML 解析基础架构知道我们的自定义元素。我们通过在两个特殊用途的属性文件中注册我们的自定义 `namespaceHandler`和自定义 XSD 文件来做到这一点。这些属性文件都放置在应用程序的`META-INF`目录中，例如，可以与 JAR 文件中的二进制类一起分发。Spring XML 解析基础结构通过使用这些特殊属性文件自动选择您的新扩展，其格式将在接下来的两节中详细介绍。

**创建`META-INF/spring.handlers`**

调用的`spring.handlers`属性文件包含 XML Schema URI 到命名空间处理程序类的映射。对于我们的示例，我们需要编写以下内容：

```
http\://www.mycompany.example/schema/myns=org.springframework.samples.xml.MyNamespaceHandler
```

（`:`字符是 Java 属性格式中的有效分隔符，因此 URI 中的`:`字符需要使用反斜杠进行转义。）

键值对的第一部分（键）是与您的自定义命名空间扩展关联的 URI，并且需要与自定义 XSD 架构中指定的`targetNamespace` 属性值完全匹配。

**编写'META-INF/spring.schemas'**

调用的属性文件`spring.schemas`包含 XML 模式位置（在使用模式作为`xsi:schemaLocation`属性的一部分的 XML 文件中与模式声明一起引用）到类路径资源的映射。需要此文件来防止 Spring 绝对必须使用需要 Internet 访问来检索模式文件的默认 `EntityResolver`。如果您在此属性文件中指定映射，Spring 会在类路径上搜索模式（在本例中， `myns.xsd`在`org.springframework.samples.xml`包中）。以下代码段显示了我们需要为自定义模式添加的行：

```
http\://www.mycompany.example/schema/myns/myns.xsd=org/springframework/samples/xml/myns.xsd
```

（记住`:`字符必须被转义。）

鼓励您将 XSD 文件（或多个文件）部署在类路径上的`NamespaceHandler`和`BeanDefinitionParser`类旁边。


---

# 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/10.-fu-lu/10.2.-zi-ding-yi-xml-schema/10.2.4.-zhu-ce-chu-li-cheng-xu-he-mo-shi.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.
