第4章 Spring XML で Camel を使用する
Spring XML
Spring XML ファイルで Camel を使用することは、Camel で XML DSL を使用する方法の 1 つです。Camel は歴史的に長い間 Spring XML を使用してきました。Spring フレームワークは、Spring アプリケーションを構築するための一般的な設定として XML ファイルから始まりました。
Spring アプリケーションの例
<camelContext id="camel-A" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="seda:start"/> <to uri="mock:result"/> </route> </camelContext>
== Configuring Components and Endpoints You can configure your Component or Endpoint instances in your Spring XML as follows in this example. [source,xml]
== Configuring Components and Endpoints
You can configure your Component or Endpoint instances in your Spring XML as follows in this example.
[source,xml]
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> </camelContext>
<bean id="jmsConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp:someserver:61616"/> </bean> <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp:someserver:61616"/> </bean> </property> </bean>
<camelContext xmlns="http://camel.apache.org/schema/spring"> <routeBuilder ref="myBuilder"/> </camelContext>
<bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>
== Using package scanning Camel also provides a powerful feature that allows for the automatic discovery and initialization of routes in given packages. This is configured by adding tags to the camel context in your spring context definition, specifying the packages to be recursively searched for `RouteBuilder` implementations. To use this feature add a <package></package> tag specifying a comma separated list of packages that should be searched. For example, [source,xml]
== Using package scanning
Camel also provides a powerful feature that allows for the automatic discovery and initialization of routes in given packages. This is configured by adding tags to the camel context in your spring context definition, specifying the packages to be recursively searched for `RouteBuilder` implementations. To use this feature add a <package></package> tag specifying a comma separated list of packages that should be searched. For example,
[source,xml]
<camelContext> <packageScan> <package>com.foo</package> <excludes>.Excluded</excludes> <includes>.*</includes> </packageScan> </camelContext>
This scans for RouteBuilder classes in the `com.foo` and the sub-packages. You can also filter the classes with includes or excludes such as: [source,xml]
This scans for RouteBuilder classes in the `com.foo` and the sub-packages.
You can also filter the classes with includes or excludes such as:
[source,xml]
<camelContext> <packageScan> <package>com.foo</package> <excludes>*.*Special</excludes> </packageScan> </camelContext>
<!-- enable Spring @Component scan -→ <context:component-scan base-package="org.apache.camel.spring.issues.contextscan"/>
<camelContext xmlns="http://camel.apache.org/schema/spring"> <!-- and then let Camel use those @Component scanned route builders -→ <contextScan/> </camelContext>
This allows you to just annotate your routes using the Spring `@Component` and have those routes included by Camel: [source,java]
This allows you to just annotate your routes using the Spring `@Component` and have those routes included by Camel:
[source,java]
@Component public class MyRoute extends RouteBuilder {
You can also use the ANT style for inclusion and exclusion, as mentioned above in the package scan section. :leveloffset!:
You can also use the ANT style for inclusion and exclusion, as mentioned above in the package scan section.
:leveloffset!: