# 9.1.3 使用 Spring Integration 的 DSL 配置

让我们进一步尝试定义文件编写集成流。这一次，仍然使用 Java 定义它，但是将使用 Spring Integration 的 Java DSL。不是为流中的每个组件声明一个单独的 bean，而是声明一个定义整个流的 bean。

{% code title="程序清单 9.4 为设计集成流提高流式 API" %}

```java
package sia5;
​
import java.io.File;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.channel.MessageChannels;
import org.springframework.integration.file.dsl.Files;
import org.springframework.integration.file.support.FileExistsMode;
​
@Configuration
public class FileWriterIntegrationConfig {
    
    @Bean
    public IntegrationFlow fileWriterFlow() {
        return IntegrationFlows
            .from(MessageChannels.direct("textInChannel"))
            .<String, String>transform(t -> t.toUpperCase())
            .handle(Files.outboundAdapter(new File("/tmp/sia5/files"))
                    .fileExistsMode(FileExistsMode.APPEND)
                    .appendNewLine(true))
            .get();
    }
}
```

{% endcode %}

这个新配置尽可能简洁，用一个 bean 方法捕获整个流。IntegrationFlows 类初始化了这个构建者 API，可以从该 API 声明流。

在程序清单 9.4 中，首先从名为 textInChannel 的通道接收消息，然后该通道转到一个转换器，使消息有效负载大写。在转换器之后，消息由出站通道适配器处理，该适配器是根据 Spring Integration 的文件模块中提供的文件类型创建的。最后，调用 get() 构建要返回的 IntegrationFlow。简而言之，这个 bean 方法定义了与 XML 和 Java 配置示例相同的集成流。

注意，与 Java 配置示例一样，不需要显式地声明通道 bean。虽然引用了 textInChannel，但它是由 Spring Integration 自动创建的，因为没有使用该名称的现有通道 bean。但是如果需要，可以显式地声明通道 bean。

至于连接转换器和外部通道适配器的通道，甚至不需要通过名称引用它。如果需要显式配置通道，可以在流定义中通过调用 channel() 的名称引用：

```java
@Bean
public IntegrationFlow fileWriterFlow() {
    return IntegrationFlows
        .from(MessageChannels.direct("textInChannel"))
        .<String, String>transform(t -> t.toUpperCase())
        .channel(MessageChannels.direct("fileWriterChannel"))
        .handle(Files.outboundAdapter(new File("/tmp/sia5/files"))
                .fileExistsMode(FileExistsMode.APPEND)
                .appendNewLine(true))
        .get();
}
```

在使用 Spring Integration 的 Java DSL（与任何流式 API 一样）时要记住的一件事是，必须巧妙地使用空白来保持可读性。在这里给出的示例中，我小心地缩进了行以表示相关代码块。对于更长、更复杂的流，甚至可以考虑将流的一部分提取到单独的方法或子流中，以获得更好的可读性。

现在已经看到了使用三种不同配置风格定义的简单流，让我们回过头来看看 Spring Integration 的全貌。


---

# 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.3-shi-yong-spring-integration-de-dsl-pei-zhi.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.
