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中。
转载于:https://blog.51cto.com/killko/1131092