命名空間支援
您可以使用 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 和綱要位置的命名一致性。