第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>
<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>
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"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="myBuilder"/>
</camelContext>
<bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>
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 { }
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 {
}
サンプルアプリケーションについては、camel-spring-boot-examples リポジトリーの XML import の例を参照してください。