Spring Java Config Example

Simple Jar runner

Want to run your jar file and supply a list of packages / configuration classes and just see it run with Camel? Try Main from org.apache.camel.spring which even supports command-line flags.

The spring java config example is a simple refactor of the spring example since Camel 2.0 to show how to use the Spring JavaConfig approach to working with Camel. In this example we just write RouteBuilder implementations, then Camel will find it through your configuration.

 

NOTE From Camel 2.2.0, camel-example-spring-javaconfig can only work with Spring 3.x.

To run the example we use the Camel Maven Plugin. For example from the source or binary distribution the following should work

cd examples/camel-example-spring-javaconfig mvn camel:run

You need to add camel-spring-javaconfig dependency into pom.xml and also set the configure class or base package in the camel plugin configuration.

<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-javaconfig</artifactId> </dependency>

Here is the configuration for camel plugin.

{snippet:id=example|lang=xml|url=camel/trunk/examples/camel-example-spring-javaconfig/pom.xml}

What this does is boot up the Spring ApplicationContext defined in the file MyRouteConfig class on the classpath. This is a regular Java file which has the Spring JavaConfig annotation to configure a CamelContext.

{snippet:id=RouteConfig|lang=java|url=camel/trunk/examples/camel-example-spring-javaconfig/src/main/java/org/apache/camel/example/spring/javaconfig/MyRouteConfig.java}

In the method of setupCamelContext(CamelContext camelContext), we setup the JMS component's connection factory and register the component into the camelcontext. You can override this method if you want to setup the another connection factory or start up a JMS broker here.

You can write the route rule with Java DSL in the route() method.

This approach, of using Java code to write the routes in the DSL and Spring will help your configure the Camel context as the Spring Java Config Example shows.

  • No labels