OSGI lueprint入门之二

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中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值