Blueprint规范理解

BluePrint提供一个依赖注入框架来实现OSGI,并且,在OSGI Compendium R4.2里被OSGI组织标准化。

他被设计用来处理OSGI的动态特性,服务可以在任何时间变成可用和不可用。Blueprint规范也被设计用来和POJO协同工作,简化组件的编写,也支持在JSE环境进行单元测试,无需知晓他们任何被组装。

Buleprint的xml文件定义和描述了不同组件的组装,它是Blueprint编程模型的关键。规范描述了组件如何获得实例,如何捆绑到一起形成一个运行模块。

Blueprint Bundles

Blueprint容器规格使用一个扩展的模式,即一个扩展的bundle,他监控框架里的bundles,根据根据bundles的状态,执行代表这些状态的动作或者方法。Blueprint 扩展bundle等待bundle们被激活,并检查他们是否是blueprint类型的bundles。一个bundle被看做blueprint bundle的标准是:他包含blueprint的xml文件。这个文件被固定的放在OSGI-INF/blueprint/目录下或者被明确地在清单文件的头部指出的目录下。

一旦扩展识别这个bundle是blueprint bundle,它创建一个blueprint容器来代表这个bundle,这个容器的功用是:

  • 解析blueprint XML文件
  • 实例化POJO组件
  • 将组件们捆绑在一起
  • 注册服务
  • 查找服务的引用

在实例化期间,blueprint容器确认强制服务引用时满意的,注册所有服务到服务注册库中,创建初始化的组件实例,blueprint扩展bundle也销毁blueprint容器当他停止时。

Blueprint XML

blueprint xml 的定义,是以blueprint元素作为顶级元素的,如下所示:

<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> ... </blueprint> xml文件的命名空间指明这个文档符合blueprint 1.0.0版本,顶级的blueprint元素指明文档作为一个buleprint模块来定义。

Beans

使用bean元素来表示beans,下面定义了一个简单的bean,名称为accountOne,实现类是:org.apache.aries.simple.Account

<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <bean id="accountOne" class="org.apache.aries.simple.Account" /> </blueprint>

Bean的建立

在对象建立期间,BluePrint 容器必须首先找到正确的构造器或者工厂方法,这个方法的参数必须与xml中定义的参数匹配。缺省情况下,BluePrint容器使用xml中参数的数量和顺序到java类中找正确的构造器或者工厂方法。如果没有匹配的,容器将试图重排参数次序并需找最匹配的方法。

帮助容器检出正确的构造器、方法或者参数安排,附加属性,注入索引或类型,可以用argument元素来指定。例如,类型属性指明一个类名,就可以用来匹配argument元素到一个确切的类型。

一个bean可以使用类构造器创建。在下面例子中,class属性指明了用来实例化bean的类名,blueprint容器将创建account对象,通过传递1作为参数,到构造器中。


 public class Account {
public Account(long number) { ... } ... }

 <bean id="accountOne" class="org.apache.aries.simple.Account"> <argument value="1"/> </bean> 一个bean被创建也可以使用静态工厂方法,在下面例子中,class属性指明了包含了静态工厂方法的类,而那个静态工厂方法,通过factory-method方法指明。Blueprint容器可以调用createAccount静态方法去创建Account对象。

一个bean也可以通过实例工厂方法创建,下面例子中,accountFactory是工厂类,容器首先创建工厂类的实例,然后容器调用createAccount方法创建Account对象。


 public class AccountFactory {
public AccountFactory(String factoryName) { ... } public Account createAccount(long number) { return new Account(number); } }

 <bean id="accountFactory" class="org.apache.aries.simple.AccountFactory"> <argument value="account factory"/> </bean> <bean id="accountThree" factory-ref="accountFactory" factory-method="createAccount"> <argument value="3"/> </bean>

Bean Properties

Bean的属性值可以被注入。bean实例被构造好后,注入立刻被执行。下面的例子创建了一个account的实例并设置description属性。


                                                                                                                                                                                                       
public class Account {
public Account(long number) { ... } public String getDescription() { ... } }

 <bean id="accountOne" class="org.apache.aries.simple.Account"> <argument value="1"/> <property name="description" value="#1 account"/> </bean> Bean Wirting

属性注入用于将beans绑定到一起,下面例子展示了accountOneCurrency 注入。

 public class Account { public Account() { ... } public void setCurrency(Currency currency) { ... } } public class Currency { public Currency() { ... } } <bean id="accountOne" class="org.apache.aries.simple.Account"> <property name="currency" ref="currency" /> </bean> <bean id="currency" class="org.apache.aries.simple.Currency" /> Services

BluePrint XML中,一个Service元素用来定义要在OSGI服务注册库中注册的服务。

提供服务对象的bean可以通过使用ref属性来引用。参看下面例子:

 public class AccountImpl implements Account { public Account() { ... } public void setCurrency(Currency currency) { ... } } <service id="serviceOne" ref="account" interface="org.apache.aries.simple.Account" /> <bean id="account" class="org.apache.aries.simple.AccountImpl" /> 也可以通过另外一种方式:

 <service id="serviceTwo" interface="org.apache.aries.simple.Account"> <bean class="org.apache.aries.simple.AccountImpl" /> </service> 下面的接口中,blueprint可以通过auto-export属性识别要注册为服务的接口。

 <service id="serviceOne" ref="account" auto-export="interfaces" /> <bean id="account" class="org.apache.aries.simple.AccountImpl" /> Service Properties

一个服务可以连通一组属性值一起被注册,这些属性值通过Service-properties子元素来指定。service-properties元素包含多个entry子元素,这些子元素代表了不同的属性。比如属性:key被指明用key,但是属性的值用value属性或者内联其他元素来指定。服务的属性值可以是不同的类型,但是只能是OSGI认可的属性类型:原始类型,原始的包装类,集合或基本类型数组。

下面的例子中,服务注册时有两个属性,一个是原始类型的包装类型Boolean,一个是mode属性,他没有指定类型,那么他就是默认的类型:String

 <service id="serviceFour" ref="account" autoExport="all-classes"> <service-properties> <entry key="active"> <value type="java.lang.Boolean">true</value> </entry> <entry key="mode" value="shared"/> </service-properties> </service>  

Service Ranking


 
服务级别:当有多个服务匹配于客户端的选择时,通过service ranking来确定到底调用哪个,高级别的优先选择,缺省的服务级别是0,可以通过ranking属性来指定:
 
<service id="serviceFive" ref="account" auto-export="all-classes" ranking="3" /> 未完待续。。。。。。。。。。。。。。。。。

你可能感兴趣的:(职场,osgi,blueprint,休闲)