Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Camel uses Spring Transactions as the default transaction handling in components like JMS and JPA
  • Camel works with Spring 2 XML processing with the Xml Configuration
  • Camel Spring XML Schema's is defined at Xml Reference
  • Camel supports a powerful version of Spring Remoting which can use powerful routing between the client and server side along with using all of the available Components for the transport
  • Camel provides powerful Bean Integration with any bean defined in a Spring ApplicationContext
  • Camel integrates with various Spring helper classes; such as providing Type Converter support for Spring Resources etc
  • Allows Spring to dependency inject Component instances or the CamelContext instance itself and auto-expose Spring beans as components and endpoints.
  • Allows you to reuse the Spring Testing framework to simplify your unit and integration testing using Enterprise Integration Patterns and Camel's powerful Mock and Test endpoints
  • From Camel 2.15 onwards Camel supports Spring Boot using the camel-spring-boot component.
  • From Camel 2.17.1 onwards Camel supports Spring Cache based Idempotent repository

Using Spring to configure the CamelContext

...

... so the declaration is:

Wiki Markup
{snippet:id=xsdlocation|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextFactoryBean.xml}
... and then use the camel: namespace prefix, and you can omit the inline namespace declaration:
Wiki Markup
{snippet:id=example5|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextFactoryBean.xml}

Advanced configuration using Spring

...

You can use Java Code to define your RouteBuilder implementations. These can be defined as beans in spring and then referenced in your camel context e.g.

Wiki Markup
{snippet:id=example5|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextRouteBuilderRef.xml}

Using <package>

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 in 1.X, requires a <package></package> tag specifying a comma separated list of packages that should be searched e.g.

...

You can allow Camel to scan the container context, e.g. the Spring ApplicationContext for route builder instances. This allow you to use the Spring <component-scan> feature and have Camel pickup any RouteBuilder instances which was created by Spring in its scan process.

Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/contextscan/SpringRouteIsComponentAnnotatedTest.xml}
This allows you to just annotate your routes using the Spring @Component and have those routes included by Camel

...

You can use Spring 2.0 XML configuration to specify your Xml Configuration for Routes such as in the following example.

Wiki Markup
{snippet:id=example|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/routingUsingCamelContextFactory.xml}

Configuring Components and Endpoints

You can configure your Component or Endpoint instances in your Spring XML as follows in this example.

Wiki Markup
{snippet:id=example|lang=xml|url=camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/jmsRouteUsingSpring.xml}
Which allows you to configure a component using some name (activemq in the above example), then you can refer to the component using activemq:[queue:|topic:]destinationName. This works by the SpringCamelContext lazily fetching components from the spring context for the scheme name you use for Endpoint URIs.

For more detail see Configuring Endpoints and Components.

Spring Cache idempotent repository

Available as of Camel 2.17.1

Code Block
languagexml
<bean id="repo" class="org.apache.camel.spring.processor.idempotent.SpringCacheIdempotentRepository">
    <constructor-arg>
       <bean class="org.springframework.cache.guava.GuavaCacheManager"/>
    </constructor-arg>
    <constructor-arg value="idempotent"/>
</bean>

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="idempotent-cache">
        <from uri="direct:start" />
        <idempotentConsumer messageIdRepositoryRef="repo" skipDuplicate="true">
            <header>MessageId</header>
            <to uri="log:org.apache.camel.spring.processor.idempotent?level=INFO&amp;showAll=true&amp;multiline=true" />
            <to uri="mock:result"/>
        </idempotentConsumer>
    </route>
</camelContext>

 

 

CamelContextAware

If you want to be injected with the CamelContext in your POJO just implement the CamelContextAware interface; then when Spring creates your POJO the CamelContext will be injected into your POJO. Also see the Bean Integration for further injections.

...