Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.

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.

© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram