OSGI Blueprint(2)

来源:互联网 发布:象形字典软件 编辑:程序博客网 时间:2024/05/16 06:59
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://killko.blog.51cto.com/3419292/1131092

 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中。

本文出自 “色声香味触法” 博客,请务必保留此出处http://killko.blog.51cto.com/3419292/1131092

0 0