使用 java 配置将 spring bean 注册为 apache camel 路由构建器
Register spring bean as an apache camel route builder with java config
apache camel documentation描述了如何用@Component和SpringRouteBuilder注册一个route builder然后跳转到xml代码做
<camelContext xmlns="http://camel.apache.org/schema/spring">
<!-- and then let Camel use those @Component scanned route builders -->
<contextScan/>
</camelContext>
如何对 java 配置进行同样的操作?我有
package x.y.camel;
@Component
public class MyRouteBuilder extends SpringRouteBuilder {...}
和
@EnableWebMvc
@EnableAutoConfiguration
@ComponentScan(basePackages = {"x.y"})
public class Application implements WebApplicationInitializer {
@Bean
public SpringCamelContext camelContext(ApplicationContext applicationContext) throws Exception {
SpringCamelContext camelContext = new SpringCamelContext(applicationContext);
return camelContext;
}
该组件由 spring 拾取并创建,该部分没问题。我可以通过 camelContext.addRoutes(new MyRouteBuilder());
注册路线。唯一缺少的是如何告诉 camel 上下文选择路由,如果它被管理为 spring bean。
您的方法不起作用,因为您没有使用 CamelContextFactoryBean 创建骆驼上下文。这是隐藏在您的 class 路径中查找 Spring Bean Camel 路由的逻辑的地方。
该问题最简单的解决方案是添加一个基于 xml 的 Spring 上下文配置,该配置引用此工厂 bean!
或者,您可以尝试从您的 Application class 调用工厂 bean(参见 link:FactoryBeans and the annotation-based configuration in Spring 3.0),但是调用来自 @Configuration
class 的工厂 bean 很棘手,因为它们都是不是为兼容性而构建的机制的一部分。特别是,由于 CamelContextFactoryBean
也在实施 InitialisingBean
。
事实证明我已经非常接近解决方案了。我所要做的就是在我已经拥有的 CamelConfiguration class 上添加一个 ComponentScan 注释。
@Configuration
@ComponentScan("x.y.camel")
public class CamelConfig extends CamelConfiguration {
}
然后从我的应用程序 class 中删除 public SpringCamelContext camelContext(ApplicationContext applicationContext)
。
就是这样 - RouteBuilder 是自动拾取的。
apache camel documentation描述了如何用@Component和SpringRouteBuilder注册一个route builder然后跳转到xml代码做
<camelContext xmlns="http://camel.apache.org/schema/spring">
<!-- and then let Camel use those @Component scanned route builders -->
<contextScan/>
</camelContext>
如何对 java 配置进行同样的操作?我有
package x.y.camel;
@Component
public class MyRouteBuilder extends SpringRouteBuilder {...}
和
@EnableWebMvc
@EnableAutoConfiguration
@ComponentScan(basePackages = {"x.y"})
public class Application implements WebApplicationInitializer {
@Bean
public SpringCamelContext camelContext(ApplicationContext applicationContext) throws Exception {
SpringCamelContext camelContext = new SpringCamelContext(applicationContext);
return camelContext;
}
该组件由 spring 拾取并创建,该部分没问题。我可以通过 camelContext.addRoutes(new MyRouteBuilder());
注册路线。唯一缺少的是如何告诉 camel 上下文选择路由,如果它被管理为 spring bean。
您的方法不起作用,因为您没有使用 CamelContextFactoryBean 创建骆驼上下文。这是隐藏在您的 class 路径中查找 Spring Bean Camel 路由的逻辑的地方。
该问题最简单的解决方案是添加一个基于 xml 的 Spring 上下文配置,该配置引用此工厂 bean!
或者,您可以尝试从您的 Application class 调用工厂 bean(参见 link:FactoryBeans and the annotation-based configuration in Spring 3.0),但是调用来自 @Configuration
class 的工厂 bean 很棘手,因为它们都是不是为兼容性而构建的机制的一部分。特别是,由于 CamelContextFactoryBean
也在实施 InitialisingBean
。
事实证明我已经非常接近解决方案了。我所要做的就是在我已经拥有的 CamelConfiguration class 上添加一个 ComponentScan 注释。
@Configuration
@ComponentScan("x.y.camel")
public class CamelConfig extends CamelConfiguration {
}
然后从我的应用程序 class 中删除 public SpringCamelContext camelContext(ApplicationContext applicationContext)
。
就是这样 - RouteBuilder 是自动拾取的。