Spring Support
Apache Camel is designed to work nicely with the Spring Framework in a number of ways.
- 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
Using Spring to configure the CamelContext
You can configure a CamelContext inside any spring.xml using the CamelContextFactoryBean. This will automatically start the CamelContext along with any referenced Routes along any referenced Component and Endpoint instances.
Using Spring you can configure Routes in two ways
Using Java Code
You can use Java Code to define your RouteBuilder implementations, then in your spring.xml you can specify the Java package names to search for (recursively) to find your routes such as in the following example.
Or if you prefer you can use the Spring 2.0 XML Namespaces approach
Or if you prefer to reference the route builder instance in the spring context
Use caution when specifying the package name as org.apache.camel
or a sub package of this. This causes Camel to search in its own packages for your routes which could cause problems.
Using Spring XML
You can use Spring 2.0 XML configuration to specify your Xml Configuration for Routes such as in the following example.
Or you can refer to camel XSD in the XML declaration:
... and then use the camel: namespace prefix, and you can omit the inline namespace declaration:
Configuring Components and Endpoints
You can configure your Component or Endpoint instances in your Spring XML as follows in this example.
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.
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.