2014-05-15 50 views
0

下面是camle-osgi示例的一个片段。我可以获得骆驼上下文,但是如何获得原始的Spring应用程序上下文?或者我应该如何获得对“mybean”的参考?骆驼OSGI:我如何获得Spring应用程序上下文?

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> 

回答

0

你不应该直接使用Spring上下文。而是使用弹簧功能来注入所需的豆子。

  1. 在spring上下文中将routebuilder定义为bean,并使用setter并注入myBean。
  2. 保留自动发现并在routebuilder中使用注释(如@Autowired)注入mybean。

第三个选项是使用getContext()。getRegistry()。注册表允许按名称或类型访问Spring上下文的所有bean。

它不会是骆驼,如果我们已经没办法了:-)

那么另一个选择是使用骆驼的Bean组件从Spring上下文访问bean:http://camel.apache.org/bean.html