命名空间支持
你可以使用与企业集成的术语和概念直接对应的 XML 元素来配置 Spring Integration 组件。在许多情况下,元素名称与《企业集成模式》这本书中的名称相匹配。
要启用 Spring Integration 的核心命名空间支持在你的 Spring 配置文件中,添加以下命名空间引用和模式映射到你的顶级 beans
元素中:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
https://www.springframework.org/schema/integration/spring-integration.xsd">
(我们已经强调了特定于 Spring Integration 的行。)
你可以选择任何在 "xmlns:" 之后的名字。我们使用 int
(集成的缩写)以保持清晰,但你可能更喜欢其他的缩写。另一方面,如果你使用 XML 编辑器或 IDE 支持,自动完成功能的可用性可能会说服你保留较长的名字以确保清晰。或者,你可以创建使用 Spring Integration 模式作为主要命名空间的配置文件,如下例所示:
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
https://www.springframework.org/schema/integration/spring-integration.xsd">
(我们强调了特定于 Spring Integration 的行。)
当使用这种替代方案时,Spring Integration 元素前不需要任何前缀。另一方面,如果您在同一配置文件中定义一个通用的 Spring bean,则 bean 元素需要一个前缀 (<beans:bean …/>
)。由于将配置文件本身模块化(基于职责或架构层)通常是一个好主意,您可能会发现在专注于集成的配置文件中使用后一种方法是合适的,因为在这些文件中很少需要通用 bean。在本文档的目的中,我们假设集成命名空间是主要的。
Spring Integration 提供了许多其他命名空间。实际上,每个提供命名空间支持的适配器类型(如 JMS、文件等)都在单独的模式中定义其元素。为了使用这些元素,需要通过 xmlns
条目添加必要的命名空间及其对应的 schemaLocation
映射。例如,以下根元素显示了几个这样的命名空间声明:
<?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"
xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
xmlns:int-ws="http://www.springframework.org/schema/integration/ws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
https://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file
https://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/integration/jms
https://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
http://www.springframework.org/schema/integration/mail
https://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
http://www.springframework.org/schema/integration/ws
https://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd">
...
</beans>
本参考手册在其对应的章节中提供了各种元素的具体示例。这里,主要需要注意的是每个命名空间 URI 和模式位置的命名一致性。