Blueprint的xml文檔的頂層結點如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <blueprint xmlns=”http://www.osgi.org/xmlns/blueprint/v1.0.0”>
- ...
- </blueprint>
在頂層結點下,你可以定義bean節點。bean節點可以定義爲bean或者bean工廠,從bean結點可以獲得bean實例,通過指定scope屬性可以決定是否返回單例的bean實例:
scope=”singleton“ 節點將在初次引用時返回一個實例,並在後續的引用中都返回這個實例。
scope=“prototype” 節點在每次引用時都返回一個新的實例。
- <bean id=”prototypeAccount” class=“com.ponder.Account”
- scope=”prototype”>
- <argument value=”4”/>
- </bean>
- <bean id=”singletonAccount” class=“com.ponder.Account”
- scope=”singleton”>
- <argument value=”5”/>
- </bean>
bean節點可以通過property子節點注入常量、bean引用、OSGI service引用。
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <!--引用osgi服務,並注入bean(com.ponder.Processor)裏 -->
- <reference id="coderService" interface="com.ponder.ICoder" timeout="0"/>
- <bean id="processor" class="com.ponder.Processor">
- <!--與這裏對應,類com.ponder.Processor裏應定義有以下屬性:
- private com.ponder.ICoder coder;
- 幷包含其setter。
- -->
- <property name="coder" ref="coderService"/>
- </bean>
- </blueprint>
上例將一個實現”com.ponder.ICoder”接口的OSGI service引用通過setCoder這個setter方法注入bean中。