簡體   English   中英

Camel OSGI:如何獲得spring應用程序上下文?

[英]Camel OSGI: how do I get the spring application context?

下面是camle-osgi示例的摘錄。 我可以獲得駱駝上下文,但是如何獲得原始的spring應用程序上下文呢? 或者我應該如何引用“ 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>

您不應該直接使用spring上下文。 而是使用spring功能注入所需的bean。

  1. 在spring上下文中將routebuilder定義為bean,並使用setter並注入myBean。
  2. 保持自動發現,並在routebuilder中使用@Autowired之類的注釋來注入mybean。

第三種選擇是使用getContext()。getRegistry()。 該注冊表允許按名稱或類型訪問spring上下文的所有bean。

如果我們已經不在選擇范圍內,那將不會是駱駝:-)

因此,另一個選擇是使用駱駝的bean組件從春季上下文訪問bean: http : //camel.apache.org/bean.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM