Spring系列学习之Spring 集成

小编 2026-06-15 阅读:219 评论:0
英文原文:https://spring.io/projects/spring-integration 目录 概述 介绍 特性 例子 Spring Boot配置 快速开始 学习 文档 指南...

英文原文:https://spring.io/projects/spring-integration

目录

概述

介绍

特性

例子

Spring Boot配置

快速开始

学习

文档

指南

示例


概述

扩展Spring编程模型以支持众所周知的企业集成模式。 Spring Integration在基于Spring的应用程序中实现轻量级消息传递,并支持通过声明适配器与外部系统集成。与Spring对远程处理,消息传递和调度的支持相比,这些适配器提供了更高级别的抽象。 Spring Integration的主要目标是提供一个简单的模型来构建企业集成解决方案,同时保持关注点的分离,这对于生成可维护,可测试的代码至关重要。

介绍

使用Spring Framework鼓励开发人员使用接口进行编码,并使用依赖注入(DI)为普通旧Java对象(PO​​JO)提供执行其任务所需的依赖项。 Spring Integration将这一概念更进一步,其中POJO使用消息传递范例连接在一起,并且各个组件可能不了解应用程序中的其他组件。这种应用程序是通过组装细粒度可重用组件来构建的,以形成更高级别的功能。通过精心设计,这些流程可以模块化,并在更高的层次上重复使用。

除了将细粒度组件连接在一起外,Spring Integration还提供多种通道适配器和网关,以便与外部系统进行通信。通道适配器用于单向集成(发送或接收);网关用于请求/回复方案(入站或出站)。有关适配器和网关的完整列表,请参阅参考文档。

Spring Cloud Stream项目建立在Spring Integration之上,其中Spring Integration用作消息驱动的微服务的引擎。

特性

  •     实施大多数企业集成模式
  •     端点Endpoint
  •     频道Channel(点对点和发布/订阅)
  •     聚合(Aggregator)
  •     过滤(Filter)
  •     变压器(Transformer)
  •     控制总线(Control Bus)
  •     ...
  •     与外部系统集成
  •     REST / HTTP
  •     FTP/ SFTP
  •     推特(Twitter)
  •     Web服务(SOAP和ReST)
  •     TCP/ UDP
  •     JMS
  •     RabbitMQ
  •     电子邮件(Email)
  •     ...
  •     该框架具有广泛的JMX支持
  •     将框架组件公开为MBean
  •     适配器从MBean获取属性,调用操作,发送/接收通知
  •  

例子

在下面的“快速入门”应用程序中,您可以看到使用相同的网关接口来调用两个完全不同的服务实现。 要构建和运行该程序,您需要如上所述的spring-integration-ws和spring-integration-xml模块。



public class Main {

	public static void main(String... args) throws Exception {
		ApplicationContext ctx =
			new ClassPathXmlApplicationContext(\"context.xml\");
		// Simple Service
		TempConverter converter =
			ctx.getBean(\"simpleGateway\", TempConverter.class);
		System.out.println(converter.fahrenheitToCelcius(68.0f));
		// Web Service
		converter  = ctx.getBean(\"wsGateway\", TempConverter.class);
		System.out.println(converter.fahrenheitToCelcius(68.0f));
	}
}



public interface TempConverter {

	float fahrenheitToCelcius(float fahren);

}



<!-- Simple Service -->

<int:gateway id=\"simpleGateway\"
	service-interface=\"foo.TempConverter\"
	default-request-channel=\"simpleExpression\" />

<int:service-activator id=\"expressionConverter\"
	input-channel=\"simpleExpression\"
	expression=\"(payload - 32) / 9 * 5\"/>

<!-- Web Service -->

<int:gateway id=\"wsGateway\" service-interface=\"foo.TempConverter\"
	default-request-channel=\"viaWebService\" />

<int:chain id=\"wsChain\" input-channel=\"viaWebService\">
	<int:transformer
	   expression=\"\'&lt;FahrenheitToCelsius xmlns=&quot;https://www.w3schools.com/xml/&quot;&gt;&lt;Fahrenheit&gt;XXX&lt;/Fahrenheit&gt;&lt;/FahrenheitToCelsius&gt;\'.replace(\'XXX\', payload.toString())\" />
	<int-ws:header-enricher>
		<int-ws:soap-action value=\"https://www.w3schools.com/xml/FahrenheitToCelsius\"/>
	</int-ws:header-enricher>
	<int-ws:outbound-gateway
		uri=\"https://www.w3schools.com/xml/tempconvert.asmx\"/>
	<int-xml:xpath-transformer
		xpath-expression=\"/*[local-name()=\'FahrenheitToCelsiusResponse\']/*[local-name()=\'FahrenheitToCelsiusResult\']\"/>
</int:chain>

 这是使用Java DSL(和Spring Boot)的相同应用程序(Web服务部分)。 如果不使用Spring Boot,则需要直接使用spring-boot-starter-integration依赖项或spring-integration-java-dsl。 如果您使用Spring Integration从5.0开始,则不需要任何其他依赖项 - Java DSL包含在核心项目中:



@Configuration
@SpringBootApplication
@IntegrationComponentScan
public class Application {

  public static void main(String[] args) {
    ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
    TempConverter converter = ctx.getBean(TempConverter.class);
    System.out.println(converter.fahrenheitToCelcius(68.0f));
    ctx.close();
  }

  @MessagingGateway
  public interface TempConverter {

    @Gateway(requestChannel = \"convert.input\")
    float fahrenheitToCelcius(float fahren);

  }

  @Bean
  public IntegrationFlow convert() {
      return f -> f
        .transform(payload ->
              \"<FahrenheitToCelsius xmlns=\\\"https://www.w3schools.com/xml/\\\">\"
            +     \"<Fahrenheit>\" + payload + \"</Fahrenheit>\"
            + \"</FahrenheitToCelsius>\")
        .enrichHeaders(h -> h
            .header(WebServiceHeaders.SOAP_ACTION,
                \"https://www.w3schools.com/xml/FahrenheitToCelsius\"))
        .handle(new SimpleWebServiceOutboundGateway(
            \"https://www.w3schools.com/xml/tempconvert.asmx\"))
        .transform(Transformers.xpath(\"/*[local-name()=\\\"FahrenheitToCelsiusResponse\\\"]\"
            + \"/*[local-name()=\\\"FahrenheitToCelsiusResult\\\"]\"));
  }

}

Spring Boot配置

Spring集成的Spring Boot自动配置

快速开始


使用Spring Initializr引导您的应用程序。

学习

文档

每个Spring项目都有自己的; 它详细解释了如何使用项目功能以及使用它们可以实现的功能。

5.1.1 CURRENT GA Reference Doc. API Doc.
5.1.2 SNAPSHOT Reference Doc. API Doc.
5.0.11 SNAPSHOT Reference Doc. API Doc.
5.0.10 GA Reference Doc. API Doc.
4.3.19 SNAPSHOT Reference Doc. API Doc.
4.3.18 GA Reference Doc. API Doc.

指南

该指南旨在在15-30分钟内完成,提供快速,实用的说明,用于为Spring的任何开发任务构建入门应用程序。

  • Integrating Data 了解如何构建使用Spring Integration获取数据,处理数据并将其写入文件的应用程序。

示例

尝试一些示例:

 

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表