领先一步
VMware提供培训和认证,助您加速进步。
了解更多就在几天前,SpringSource Tool Suite™ (STS) 2.9.1发布了。除了包含许多新特性外,它还提供了几项对Spring Integration用户来说尤其令人兴奋的功能。首先,最新的STS版本增加了对Spring Integration 2.1的支持,并改进了Spring Integration流程的可视化功能。STS现在支持所有最近添加的Spring Integration适配器,例如:
此外,所有现有的Spring Integration适配器都已更新,以支持新的可视化元素。对Spring Integration用户来说,另一个很棒的新增功能是SpringSource Tool Suite 2.9.x现在内置了Spring Integration的模板支持。因此,当使用Spring模板项目向导创建新项目时,您现在可以在以下3个针对Spring Integration的新模板之间进行选择:
[caption id="attachment_10681" align="aligncenter" width="342" caption="模板选择屏幕"][/caption]
该屏幕提供了所有可用Spring模板的列表,包括前面提到的3个针对Spring Integration的新模板。如果您看到模板名称前面有一个小箭头,这意味着实际的模板尚未下载。一旦下载,模板将缓存在您的机器上,您无需再次下载模板文件,除非您按下刷新按钮并且远程有新版本的模板可用。
让我们选择模板Spring Integration Project (Standalone) - Simple并按下“下一步”。如果模板尚未下载,您将看到以下弹出窗口:
继续后,模板下载完成后,您将看到Project Settings Screen(项目设置屏幕)。在那里,您必须提供项目名称以及一些属性,这些属性有助于轻松将新创建的项目部署到Maven仓库。
[caption id="attachment_10687" align="aligncenter" width="420" caption="项目设置屏幕"][/caption]
事实上,除了创建Eclipse特定的项目文件外,此模板向导还会生成一个项目pom.xml文件,这将使该项目也能轻松地在STS之外使用(例如,将其作为您的持续集成过程的一部分,使用诸如Jenkins等工具构建)。因此,除了项目名称外,您还需要提供groupId、artifactId和version属性。最后,您还需要定义一个顶层(基本)包,您的相关源文件将位于该包下。
按下“Finish”后,项目将被创建,并应该在STS的Package Explorer窗口左侧列出。至此,您现在拥有了一个功能齐全的Spring Integration项目。
除了使用STS,您还可以非常简单地使用Maven从命令行启动项目。只需进入项目目录并执行:
$ mvn clean package exec:java
此命令将清除target构建目录,编译并打包应用程序,然后使用Exec Maven Plugin执行应用程序。应用程序将像在STS中一样启动。按下“q”将退出应用程序。
为了实现这一点,使用了Spring Integration。然而,如果您查看启动Spring应用程序上下文的Main类的详细信息,您会注意到它本身并没有引用Spring Integration。主类基本上所做的就是从Spring Application Context中获取“StringConversionService”接口的一个实例。不过,这个接口被Spring Integration的Gateway使用,用于捕获输入的字符串并将其转发到请求通道。
项目的应用程序上下文XML文件位于META-INF/spring/integration目录下。如您所见,应用程序由一个非常基本的Spring Integration流程组成。我们从一个简单的Gateway开始,它使用了前面提到的StringConversionService接口。
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd">
<int:gateway id="gateway"
default-request-timeout="5000"
default-reply-timeout="5000"
default-request-channel="requestChannel"
default-reply-channel="replyChannel"
service-interface="org.springintegration.demo.service.StringConversionService">
<int:method name="convertToUpperCase"/>
</int:gateway>
<int:service-activator id="serviceActivator"
input-channel="requestChannel"
output-channel="replyChannel"
expression="payload.toUpperCase()"/>
<int:channel id="replyChannel"/>
<int:channel id="requestChannel"/>
</beans>
定义了两个消息通道。第一个通道将包含输入字符串的消息路由到服务激活器。服务激活器本身将使用Spring表达式语言(SpEL)将输入消息的有效载荷转换为大写。然后通过回复通道将大写字符串返回给调用者并打印到命令行。
在实际场景中,Service Activator很可能会引用另一个服务Bean来执行一些非平凡的业务逻辑,例如:
…
<int:service-activator id="serviceActivator"
input-channel="requestChannel"
output-channel="replyChannel"
ref="businessService"
method="myMethodToExecute"/>
…
此外,请注意,在此模板中使用Service Activator是为了说明如何执行其他基于Java的业务服务。如果您的目标仅仅是转换Spring Integration消息的有效载荷,也请考虑使用Transformer,这在语义上可能更合适。
这是一个非常简单的用例,但它为您提供了一个很好的起点来实现您自己的Spring Integration组件。我们期望这些模板将大大降低学习和体验在您自己的项目中使用Spring Integration的门槛。如果您有任何疑问,我们希望听到您的声音。如有任何问题,请访问Spring Integration论坛:forum.springsource.org。关于Spring Integration的更多一般信息,也请访问我们的项目网站:http://www.springintegration.org/。