Babel Camel using the Spring Application ContextΒΆ

This page explains how to integrate a Babel Camel route with a Spring Application Context.

A route defined with the Babel Camel DSL may be transparently loaded into a Spring Application Context, such as that:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~  Copyright 2010-2014 Crossing-Tech SA, EPFL QI-J, CH-1015 Lausanne, Switzerland.
  ~  All rights reserved.
  ~
  ~ ==================================================================================
  -->

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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" id="babel-camel-context">
        <packageScan>
            <package>io.xtech.babel.camel.builder.spring</package>
        </packageScan>
    </camelContext>

</beans>

With the corresponding Babel Camel route:

package io.xtech.babel.camel.builder.spring

import io.xtech.babel.camel.builder.RouteBuilder

class MyRouteBuilder extends RouteBuilder {
  from("direct:babel-rb-1").routeId("route1").to("mock:babel-rb")
  from("direct:babel-rb-2").routeId("route2").to("mock:babel-rb")
}

Warning

Unfortunately, the injection using setters may cause Babel initialization to fail because Babel may get intialized before every required Spring Bean. To handle this, please use the io.xtech.cf.babel.camel.builder.SpringRouteBuilder and define your route in the configure method body. You may also use the constructor injection if you prefer to rely on the basic io.xtech.babel.camel.builder.RouteBuilder.

import org.springframework.beans.factory.annotation.Autowired

import scala.beans.BeanProperty

class SetterInjectionRouteBuilder extends SpringRouteBuilder {

  @Autowired
  @BeanProperty
  var aBean: MyBeanProcessor = _

  def configure(): Unit = {
    from("direct:babel-rb-setter").as[String].
      processBody(aBean.doSomething).
      to("mock:babel-rb-setter")
  }
}