# 9.1.1 使用 XML 定义集成流

尽管在本书中我避免使用 XML 配置，但 Spring Integration 在 XML 中定义的集成流方面有着悠久的历史。因此，我认为值得至少展示一个 XML 定义的集成流示例。下面的程序清单显示了如何在 XML 中配置流。

{% code title="程序清单 9.2 使用 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"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-file="http://www.springframework.org/schema/integration/file"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/integration
           http://www.springframework.org/schema/integration/spring-integration.xsd
           http://www.springframework.org/schema/integration/file
           http://www.springframework.org/schema/integration/file/
                           springintegration-file.xsd">
    
    <int:channel id="textInChannel" />
    <int:transformer id="upperCase" input-channel="textInChannel"
          output-channel="fileWriterChannel" expression="payload.toUpperCase()" />
    <int:channel id="fileWriterChannel" />
    <int-file:outbound-channel-adapter id="writer" channel="fileWriterChannel"
          directory="/tmp/sia5/files" mode="APPEND" append-new-line="true" />
</beans>
```

{% endcode %}

分析一下程序清单 9.2 中的 XML：

* 配置了一个名为 textInChannel 的通道，这与为 FileWriterGateway 设置的请求通道是相同的。当在 FileWriterGateway 上调用 writeToFile() 方法时，结果消息被发布到这个通道。
* 配置了一个转换器来接收来自 textInChannel 的消息。它使用 Spring Expression Language（SpEL）表达式在消息有效负载上调用 toUpperCase()。然后将大写操作的结果发布到 fileWriterChannel 中。
* 配置了一个名为 fileWriterChannel 的通道，此通道用作连接转换器和外部通道适配器的管道。
* 最后，使用 int-file 命名空间配置了一个外部通道适配器。这个 XML 命名空间由 Spring Integration 的文件模块提供，用于编写文件。按照配置，它将接收来自 fileWriterChannel 的消息，并将消息有效负载写到一个文件中，该文件的名称在消息的 file\_name 头中指定，该文件位于 directory 属性中指定的目录中。如果文件已经存在，则将用换行来追加文件，而不是覆盖它。

如果希望在 Spring Boot 应用程序中使用 XML 配置，则需要将 XML 作为资源导入 Spring 应用程序。最简单的方法是在应用程序的 Java 配置类上使用 Spring 的 @ImportResource 注解：

```java
@Configuration
@ImportResource("classpath:/filewriter-config.xml")
public class FileWriterIntegrationConfig { ... }
```

尽管基于 XML 的配置很好地服务于 Spring Integration，但大多数开发人员对使用 XML 越来越谨慎。（正如我所说的，我在本书中避免使用 XML 配置）让我们把这些尖括号放在一边，将注意力转向 Spring Integration 的 Java 配置风格。


---

# 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-v5/di-9-zhang-ji-cheng-spring/9.1-sheng-ming-jian-dan-de-ji-cheng-liu/9.1.1-shi-yong-xml-ding-yi-ji-cheng-liu.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.
