Spring Integration 3.0.2 和 4.0 Milestone 4 已發佈

發佈 | Artem Bilan | 2014 年 3 月 31 日 | ...

我們很高興地宣佈 Spring Integration 4.0 的最終里程碑版本,以及 3.0.x 系列的下一個維護版本。 3.0.2.RELEASE 包含 3.0 版本的一些重要修正。 建議 Spring Integration 3.0 使用者儘快升級到此版本。 請參閱 3.0.2 發佈說明專案頁面 以取得更多資訊。

Spring Integration 4.0 是該框架的下一代版本,現在基於新的 Spring Framework 4.0 Messaging Module。 有關將應用程式從 Spring Integration 3.0 遷移到 4.0 的資訊,請參閱遷移指南

Spring Integration 4.0 發佈的另一個主要目標是為框架新增改進的 Java 和註解組態功能;讓我們闡明其中的一些功能...

@EnableIntegration

Spring Integration 提供了許多環境和內建 Bean 來支援執行時間 企業整合模式 和訊息傳遞基礎架構。 使用 XML 組態時,它們會根據 NamespaceHandler 的需要自動宣告。 在純 Java 組態中,沒有命名空間處理常式,需要另一種機制來設定整合環境。 已為此目的新增 @EnableIntegration 註解。 它類似於 spring-webmvc 中的 @EnableWebMvc 或 Spring Data 中的 @Enable*Repositories 註解,並且應與至少一個類別上的 @Configuration 註解一起放置。

請注意,在 ApplicationContext 中只需要一個 @EnableIntegration 註解。 放置註解後,您可以開始從 Spring @Configuration 類別設定整合流程

@Configuration
@EnableIntegration
public static class MyConfiguration {

    @Bean
    public MessageChannel fileWritingChannel() {
         return new DirectChannel();
    }

    @Bean
    public FileWritingMessageHandler fileWritingMessageHandler() {
        return new FileWritingMessageHandler(this.outputDir);
    }

   @Bean
    public ConsumerEndpointFactoryBean fileWritingEndpoint() {
        ConsumerEndpointFactoryBean endpoint = new ConsumerEndpointFactoryBean();
        endpoint.setHandler(this.fileWritingMessageHandler());
        endpoint.setInputChannel(this.fileWritingChannel());
        return endpoint;
    }

}

當然,透過元件掃描,現有的 Spring Integration 組態註解(@MessageEndpoint@ServiceActivator@Router@Filter 等)可用於定義流程。 有關範例,請參閱本文稍後的 Spring Boot 應用程式。

@MessagingGateway

另一個有用且重要的訊息傳遞元件是 Messaging Gateway。 使用 XML 時,我們使用 <int:gateway/> 元件來提供介面的實作,作為訊息傳遞流程的閘道。 使用 Spring Integration 4.0,您可以使用新引入的 @MessagingGateway 註解來避免 XML 組態。 此註解提供與 <int:gateway/> 元素相同的屬性,並放置在閘道的服務介面上

@MessagingGateway(defaultRequestChannel = "gatewayChannel",
     defaultHeaders = @GatewayHeader(name = "foo", value = "FOO"))
public interface MyGateway {

	@Gateway(headers = @GatewayHeader(name = "calledMethod",
                                      expression = "#gatewayMethod.name"))
	String echo(String payload);

}

重要:由於此元件不會自動對 Spring 容器可見,並且預設的 @ComponentScan 無法與介面搭配使用,因此引入了另一個新的註解 @IntegrationComponentScan。 此註解類似於 Spring Data 中的 @Enable*Repositories,並提供選項來設定 basePackages 屬性以掃描整合元件,並且應與 @Configuration 一起放置。

Spring Boot @EnableAutoConfiguration

利用 SpringFactoriesLoader 機制,Spring Integration 基礎架構也可透過 Spring Boot @EnableAutoConfiguration 註解使用。 只需將 Spring Integration 4.0 新增到類別路徑,然後使用 Spring Boot 自動組態功能即可。

這是一個非常簡單的 Spring Boot 應用程式

@EnableAutoConfiguration  // enables integration infrastructure
@IntegrationComponentScan // looks for gateways
@ComponentScan			  // looks for Spring Beans
public class Integration {

	public static void main(String[] args) throws Exception {
		ConfigurableApplicationContext ctx = SpringApplication.run(Integration.class, args);
		String reply = ctx.getBean(GW.class).sendAndReceive("foo");
		System.out.println(reply);
		ctx.close();
	}

	@MessagingGateway(defaultRequestChannel="in")
	public interface GW {

		String sendAndReceive(String payload);
	}

	@MessageEndpoint
	public static class MyService {

		@ServiceActivator(inputChannel="in")
		public String foo(String payload) {
			return payload.toUpperCase();
		}
	}

}

其他變更

此外,還引入了其他適用於 Java 和註解組態的有用且方便的元件:@EnableMessageHistory@EnablePublisher@EnableIntegrationMBeanExport@GlobalChannelInterceptor@IntegrationConverter 等。 有關新功能和變更的資訊,請參閱 Spring Integration 4.0 Milestone 4 的 新功能發佈說明

有關 Spring Integration 4.0 中的完整變更清單,請參閱每個里程碑的發佈說明

(第一個里程碑只是重構 3.0 程式碼以使用 spring-messaging 類別)。

4.0.0.M4 版本現在已在 Spring 里程碑存放庫中提供。

我們期待收到您的意見和回饋:Spring 論壇StackOverflow(spring-integration 標籤)、Spring JIRA

預告

Spring Integration Java DSL 即將推出!

取得 Spring 電子報

透過 Spring 電子報保持聯繫

訂閱

領先一步

VMware 提供培訓和認證,以加速您的進度。

了解更多

取得支援

Tanzu Spring 在一個簡單的訂閱中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位檔案。

了解更多

即將舉行的活動

查看 Spring 社群中所有即將舉行的活動。

查看全部