Camel doesn't register beans automatically for a Spring Framework (not Spring Boot) annotation-defined application

18 views Asked by At

My Spring Framework application has the following beans defined (among others):

@Bean
public SpringCamelContext camelContext() {
    SpringCamelContext retval = new SpringCamelContext();
    retval.setLoadTypeConverters(true);
    return retval;
}

@Bean
public LifecycleStrategy myLifecycleStrategy() {
    return LifecycleStrategySupport.adapt( (OnCamelContextInitializing) onContextInitializing ->
        System.out.println("My listener invoked"));
}

@Bean
public RouteBuilder camelRouteBuilder(
        SpringCamelContext theSpringCamelContext, ApplicationContext theApplicationContext) {
    return new CamelRouteLoader(theSpringCamelContext, theApplicationContext);
}

And our code Camel context binding to the app context and start:

myModuleCtx = getAnnotationConfigApplicationContext();
myModuleCtx.setParent(myApplicationContext);
myModuleCtx.refresh();

SpringCamelContext springCamelContext = myModuleCtx.getBean(SpringCamelContext.class);
    
RouteBuilder camelRouteBuilder = myModuleCtx.getBean(RouteBuilder.class);
springCamelContext.addRoutes(camelRouteBuilder);
    
springCamelContext.start();

but that LifecycleStarategy is never called. Also, note that routes are explicitly added to the context, instead of being picked up automatically by Camel from the bean definition.

I understand that, if running Spring Boot, these beans would be picked up by Camel automatically, so what would be the equivalent way for a Spring Framework annotated environment?

0

There are 0 answers