第6章 Spring XML での Camel の使用


Spring XML ファイルで Camel を使用して、Camel で XML DSL を使用できます。Camel は歴史的に長い間 Spring XML を使用してきました。Spring フレームワークは、Spring アプリケーションを構築するための一般的な設定として XML ファイルから始まりました。

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>
Copy to Clipboard

6.1. Spring XML ファイルでの Java DSL の使用

Java コードを使用して RouteBuilder 実装を定義できます。これらは Spring で Bean として定義され、次に示すように Camel コンテキストで参照されます。

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

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

6.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 {
}
Copy to Clipboard

ヒント

サンプルアプリケーションについては、camel-spring-boot-examples リポジトリーの XML import の例を参照してください。

トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat