OSGI Blueprint入門之二

 Blueprint的xml文檔的頂層結點如下: 

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <blueprint xmlns=”http://www.osgi.org/xmlns/blueprint/v1.0.0”>  
  3.     ...  
  4. </blueprint>  



    在頂層結點下,你可以定義bean節點。bean節點可以定義爲bean或者bean工廠,從bean結點可以獲得bean實例,通過指定scope屬性可以決定是否返回單例的bean實例: 

    scope=”singleton“  節點將在初次引用時返回一個實例,並在後續的引用中都返回這個實例。 

    scope=“prototype”  節點在每次引用時都返回一個新的實例。 

 
  1. <bean id=”prototypeAccount” class=“com.ponder.Account”   
  2.          scope=”prototype”>  
  3.        <argument value=”4”/>  
  4.    </bean>  
  5.   
  6.    <bean id=”singletonAccount” class=“com.ponder.Account”   
  7.          scope=”singleton”>  
  8.        <argument value=”5”/>  
  9.    </bean>  


     bean節點可以通過property子節點注入常量、bean引用、OSGI service引用。 

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd" default-timeout="0">  
  3.     <!--引用osgi服務,並注入bean(com.ponder.Processor)裏 -->  
  4.     <reference id="coderService" interface="com.ponder.ICoder" timeout="0"/>  
  5.     <bean id="processor" class="com.ponder.Processor">  
  6.         <!--與這裏對應,類com.ponder.Processor裏應定義有以下屬性:  
  7.         private com.ponder.ICoder coder;  
  8.         幷包含其setter。  
  9.         -->  
  10.         <property name="coder" ref="coderService"/>  
  11.     </bean>  
  12.       
  13. </blueprint>  



    上例將一個實現”com.ponder.ICoder”接口的OSGI service引用通過setCoder這個setter方法注入bean中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章