搜索

第 4 章 使用带有 Spring XML 的 Camel

download PDF

将 Camel 与 Spring XML 文件搭配使用是一种在 Camel 中使用 XML DSL 的方法。Camel 过去一直在长期使用 Spring XML。Spring 框架启动了 XML 文件,作为构建 Spring 应用程序的常用配置。

Spring 应用程序示例

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    ">

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:a"/>
            <choice>
                <when>
                    <xpath>$foo = 'bar'</xpath>
                    <to uri="direct:b"/>
                </when>
                <when>
                    <xpath>$foo = 'cheese'</xpath>
                    <to uri="direct:c"/>
                </when>
                <otherwise>
                    <to uri="direct:d"/>
                </otherwise>
            </choice>
        </route>
    </camelContext>

</beans>

4.1. 在 Spring XML 文件中使用 Java DSL

您可以使用 Java Code 定义 RouteBuilder 实施。它们在 spring 中被定义为 beans,然后在 camel 上下文中引用,如下所示:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <routeBuilder ref="myBuilder"/>
</camelContext>

<bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>

4.1.1. 配置 Spring Boot 应用程序

要将 Spring Boot Autoconfigure XML 路由用于 Bean,您需要导入 XML 资源。为此,您可以使用 Configuration 类。

例如,如果 Spring XML 文件位于 src/main/resources/camel-context.xml,您可以使用以下配置类来加载 camel-context:

示例:使用 Configuration

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;


/**
 * A Configuration class that import the Spring XML resource
 */
@Configuration
// load the spring xml file from classpath
@ImportResource("classpath:camel-context.xml")
public class CamelSpringXMLConfiguration {
}

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.