OSGi 系列(十)之 Blueprint

blueprint 是 OSGi 的一个规范,类似于 spring 的 IOC,用来处理 OSGi 的动态特性,可以大大简化服务的使用。

blueprint 是以 xml 文档来构建应用,但它也有采用 Annotation 的方式,我们在此只介绍 xml 的方式。

在 bundle 里,这个 xml 默认的位置在 OSGi-INF/blueprint 下,也可以在 MANIFEST.MF 里指定其它位置上的 xml 文档。

Bundle-Blueprint: OSGI-INF/blueprint/blueprint.xml

1. Blueprint XML

<?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 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <!--1. 默认构造函数-->
<bean id="obj" class="java.lang.Object" init-method="init" destroy-method="destroy">
<property name="name" value="MP4" />
<property name="type" ref="type1" />
</bean> <!--2. 构造函数-->
<bean id="obj" class="java.lang.Object" init-method="init" destroy-method="destroy">
<argument value="..."/>
<argument value="..."/>
</bean> <!--3. 工厂方法-->
<bean id="obj2" factory-ref="factory" factory-method="method"/>
</blueprint>

blueprint 的使用方法与 spring 差不多。

2. blueprint 的环境管理器注入

Blueprint Container 规范还定义了许多特殊的环境管理器,它们设置 ID 并提供对环境组件的访问。它们不具有 XML 定义,并且也不能被重写,因为它们的 ID 被保护起来,不能被其他管理器使用。环境管理器提供的对象只能被注入到使用引用的其他管理器中。Blueprint Container 规范定义了 4 种环境管理器:

  • blueprintBundle 提供包的 Bundle(org.osgi.framework.Bundle) 对象。
  • blueprintBundleContext 提供包的 BundleContext(org.osgi.framework.BundleContext) 对象。
  • blueprintContainer 为包提供 BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) 对象。
  • blueprintConverter 为包提供 Converter(org.osgi.service.blueprint.container.Converter) 对象,提供了对 Blueprint Container 类型转换工具的访问
<bean id="" class="" init-method="">
<property name="bundle" ref="blueprintBundle" />
<property name="bundleContext" ref="blueprintBundleContext" />
</bean>

3. blueprint 复杂属性注入

<bean class="com.edu.osgi.blueprint.Order" init-method="init">
<property name="id" value="20160320" />
<property name="products">
<!--1. 集合、数组的注入-->
<list>
<ref component-id="mp4"/>
<ref component-id="dianshi"/>
</list>
</property>
<property name="map">
<!--2. map的注入-->
<map>
<entry key="createDate" value="20160320" />
<entry key="userId" value="101" />
</map>
</property>
</bean>

4. blueprint 注册、使用服务

(1) 新建 3 个 bundle,目录结构如下:

(2) blueprint-api 为接口

package com.github.binarylei.email.api;

public interface EmailService {
void sendEmail(String to, String title, String content);
}

(3) blueprint-service 发布服务

package com.github.binarylei.email.service;

import com.github.binarylei.email.api.EmailService;

public class EmailServiceImpl implements EmailService {

    public void sendEmail(String dest, String title, String content) {
System.out.println("OSGi email send. dest=" + dest + ",title=" + title + ",content=" + content);
}
}

在 OSGI-INF/blueprint 新建 blueprint.xml 文件,发布服务:

<?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 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <bean id="emailServiceImpl" class="com.github.binarylei.email.service.EmailServiceImpl"/> <service interface="com.github.binarylei.email.api.EmailService" ref="emailServiceImpl">
<service-properties>
<entry key="vendor" value="163"/>
</service-properties>
</service>
</blueprint>

(3) blueprint-client 处理服务

package com.github.binarylei.email.client;

import com.github.binarylei.email.api.EmailService;

public class EmailClient {

    private EmailService emailService;

    public EmailService getEmailService() {
return emailService;
} public void setEmailService(EmailService emailService) {
this.emailService = emailService;
} public void init() {
emailService.sendEmail("binarylei@qq.com", "blueprint", "成功了");
}
}

在 OSGI-INF/blueprint 新建 blueprint.xml 文件,接收服务:

<?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 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <!--服务的引用-->
<reference id="emailService" interface="com.github.binarylei.email.api.EmailService"
filter="(vendor=163)"/>
<bean id="client" class="com.github.binarylei.email.client.EmailClient" init-method="init">
<property name="emailService" ref="emailService"/>
</bean>
</blueprint>

(4) 测试结果如下:

5. blueprint 服务的跟踪

(1) 在 blueprint-client 中编写一个监听类 EmailServiceListener

package com.github.binarylei.email.client;

import com.github.binarylei.email.api.EmailService;
import org.osgi.framework.ServiceReference; import java.util.Map; public class EmailServiceListener { // member-type="service-object"
public void register(EmailService emailService, Map<?, ?> properties) {
System.out.println("======register=====");
System.out.println(properties);
emailService.sendEmail("binarylei@qq.com", "blueprint", "成功了");
} /*public void register(EmailService emailService) {
}*/ // member-type="service-reference"
/*public void register(ServiceReference reference) {
}*/ public void unregister(EmailService emailService, Map<?, ?> properties) {
System.out.println("======unregister=====");
System.out.println(properties);
emailService.sendEmail("binarylei@qq.com", "blueprint", "成功了");
}
}

(2) blueprint.xml 配制文件

<!--服务的跟踪-->
<reference-list interface="com.github.binarylei.email.api.EmailService" member-type="service-object">
<reference-listener ref="emailServiceListener" bind-method="register" unbind-method="unregister"/>
</reference-list>
<bean id="emailServiceListener" class="com.github.binarylei.email.client.EmailServiceListener"/>

(3) 测试结果如下:

OSGi 系列(十)之 Blueprint的更多相关文章

  1. OSGi 系列(十八)之 基于注解的 Blueprint

    OSGi 系列(十八)之 基于注解的 Blueprint 1. 注解实现 blueprint 第一步:bundle 添加 Bundle-Blueprint-Annotation <plugin& ...

  2. OSGi 系列(十四)之 Event Admin Service

    OSGi 系列(十四)之 Event Admin Service OSGi 的 Event Admin 服务规范提供了开发者基于发布/订阅模型,通过事件机制实现 Bundle 间协作的标准通讯方式. ...

  3. OSGi 系列(十六)之 JDBC Service

    OSGi 系列(十六)之 JDBC Service compendium 规范提供了 org.osgi.service.jdbc.DataSourceFactory 服务 1. 快速入门 1.1 环境 ...

  4. OSGi 系列(十二)之 Http Service

    OSGi 系列(十二)之 Http Service 1. 原始的 HttpService (1) 新建 web-osgi 工程,目录结构如下: (2) HomeServlet package com. ...

  5. OSGi 系列(十三)之 Configuration Admin Service

    OSGi 系列(十三)之 Configuration Admin Service OSGi 的 CM 就是 Configuration Admin Service,是用于管理 Bundle 属性.并在 ...

  6. OSGi 系列(七)之服务的监听、跟踪、声明等

    OSGi 系列(七)之服务的监听.跟踪.声明等 1. OSGi 服务的事件监听 和 bundle 的事件监听类似,服务的事件监听是在服务注册.注销,属性被修改的时候,OSGi 框架会发出各种不同的事件 ...

  7. OSGi 系列(六)之服务的使用

    OSGi 系列(六)之服务的使用 1. 为什么使用服务 降低服务提供者和服务使用者直接的耦合,这样更容易重用组件 隐藏了服务的实现细节 支持多个服务的实现.这样你可以互换这实现 2. 服务的使用 2. ...

  8. OSGi 系列(一)之什么是 OSGi :Java 语言的动态模块系统

    OSGi 系列(一)之什么是 OSGi :Java 语言的动态模块系统 OSGi 的核心:模块化.动态.基于 OSGi 就可以模块化的开发 java 应用,模块化的部署 java 应用,还可以动态管理 ...

  9. Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】

    2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...

随机推荐

  1. C# List<string>和ArrayList用指定的分隔符分隔成字符串

    原文地址:https://www.cnblogs.com/ahwwmb/p/4166707.html 串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符 List<string> ...

  2. 网络命令ping/netstat/ipconfig/arp/tracert/nbstat

    1.1 Ping命令的使用 ping检测网络故障步骤: ping 127.0.0.1 ping环绕指针检测是否在计算上anzhaung了TCP/IP协议及配置是否正确 ping本机IP这个命令被被送到 ...

  3. License分类 + 引入开源软件时License的注意事项

    License分类 GPL: linux.openJDK,openJFX,mysql 融合感染,单独子模块不感染(自己的模块与引入模块的通信方式是socket) openJDK(GNU General ...

  4. MacBook Pro 一月使用体验

    从 2013 年开始,就特别想买 MBP,终于在 2015 年的尾巴用上了 MBPR.原本是要在使用一周后写一份使用体验的,但因为懒,现在拖到一个月了,刚好现在也是2016年的一月,就把标题改成一月使 ...

  5. java二维数组的长度

    //多少行 a.length //多少列 a[i].length

  6. maven项目--Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderListener

    转自:https://yq.aliyun.com/ziliao/597445 Eclipse中tomcat部署工程启动后报错: 二月 25, 2016 2:34:00 下午 org.apache.to ...

  7. 前端-CSS-11-Z-index

    ---- z-index 这个东西非常简单,它有四大特性,每个特性你记住了,页面布局就不会出现找不到盒子的情况. z-index 值表示谁压着谁,数值大的压盖住数值小的, 只有定位了的元素,才能有z- ...

  8. C#--Winform项目核心模块--考勤模块

    C#--Winform项目核心模块--考勤模块(一) C#--Winform项目核心--考勤模块(二) C#--Winform项目核心模块--考勤模块(三)

  9. L2tp协议简单解析

    1.L2TP简介 L2TP(Layer 2 Tunneling Protocol,二层隧道协议)是VPDN(Virtual PrivateDial-up Network,虚拟私有拨号网)隧道协议的一种 ...

  10. JQUERY框架的优点与面试题

    1 你觉得 jquery 有哪些好处?jQuery 是轻量级的 javascript 框架强大的选择器出色的 DOM 操作的封装可靠的事件处理机制完善的 ajax 封装出色的浏览器的兼容性支持链式操作 ...