Spring Integration 3.0.2 和 4.0 里程碑 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 模块。 有关将应用程序从 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!

预告

Spring Integration Java DSL 即将推出!

获取 Spring 新闻通讯

通过 Spring 新闻通讯保持联系

订阅

领先一步

VMware 提供培训和认证,以加速您的进步。

了解更多

获取支持

Tanzu Spring 在一个简单的订阅中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件。

了解更多

即将发生的事件

查看 Spring 社区中的所有即将发生的事件。

查看全部