第7章 Using Camel with Spring XML
Using Camel with Spring XML files is a way of using XML DSL with Camel. Camel has historically been using Spring XML for a long time. The Spring framework started with XML files as a popular and common configuration for building Spring applications.
Example of Spring application
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<route>
<from uri="direct:a"/>
<choice>
<when>
<xpath>$foo = 'bar'</xpath>
<to uri="direct:b"/>
</when>
<when>
<xpath>$foo = 'cheese'</xpath>
<to uri="direct:c"/>
</when>
<otherwise>
<to uri="direct:d"/>
</otherwise>
</choice>
</route>
</camelContext>
</beans>
7.1. Using Java DSL with Spring XML files リンクのコピーリンクがクリップボードにコピーされました!
You can use Java Code to define your RouteBuilder implementations. These are defined as beans in spring and then referenced in your camel context, as shown:
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="myBuilder"/>
</camelContext>
<bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>
7.1.1. Configure Spring Boot Application リンクのコピーリンクがクリップボードにコピーされました!
To use Spring Boot Autoconfigure XML routes for beans, you musy import the XML resource. To do this, you can use a Configuration class.
For example, given that the Spring XML file is located to src/main/resources/camel-context.xml you can use the following configuration class to load the camel-context:
Example: using a Configuration class
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* A Configuration class that import the Spring XML resource
*/
@Configuration
// load the spring xml file from classpath
@ImportResource("classpath:camel-context.xml")
public class CamelSpringXMLConfiguration {
}
For a sample application, see the XML import example in the camel-spring-boot-examples repository.