領先一步
VMware 提供培訓和認證,以加速您的進步。
瞭解更多我們很高興地宣佈 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 模組。 有關將應用程式從 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 里程碑 4 的發行說明。
有關 Spring Integration 4.0 中的完整更改列表,請參閱每個里程碑的發行說明
(第一個里程碑只是重構 3.0 程式碼以使用 spring-messaging
類)。
4.0.0.M4 版本現在可以在 Spring 里程碑儲存庫中使用。
我們期待收到您的評論和反饋:Spring 論壇、StackOverflow(spring-integration
標籤)、Spring JIRA!
預告