名稱空間支援
您可以使用 XML 元素來配置 Spring Integration 元件,這些元素直接對映到企業整合的術語和概念。在許多情況下,元素名稱與《企業整合模式》書籍中的名稱一致。
要在您的 Spring 配置檔案中啟用 Spring Integration 的核心名稱空間支援,請在頂層 '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
(Integration 的縮寫),但您可能更喜歡其他縮寫。另一方面,如果您使用 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 和模式位置命名的一致性。