Question

Below is a snippet from the camle-osgi example. I can obtain the camel context but how do I get the original spring application context? Or how am I supposed to get a reference to "mybean"?

MyRouteBuilder.java:

public class MyRouteBuilder extends RouteBuilder {
  public static void main(String[] args) throws Exception{
    new Main().run(args);
  }

  public void configure() {
    // set up the transform bean
    ModelCamelContext mc = getContext();//ok, I can get the camel context fine
    //but how to get the spring context to get to "mybean"???

    //set up routes
  }
}

beans.xml:

<beans>
 <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
    <package>org.apache.servicemix.examples.camel</package>
              -----
 </route>
 </camel:camelContext>

 <bean id="mybean" class="myclass"/>

</beans>
Was it helpful?

Solution

You should not use the spring context directly. Instead use the spring features to inject the beans you need.

  1. Define the routebuilder as a bean in the spring context and use a setter and to inject myBean.
  2. Keep the auto discovery and use annotations like @Autowired in the routebuilder to inject mybean.

A third option is to use getContext().getRegistry(). The registry allows to access all beans of the spring context by name or by type.

It would not be camel if we are already out of options :-)

So another options is to use the bean component of camel to access beans from the spring context: http://camel.apache.org/bean.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top