---
title: "OSGI笔记"
author: "ryanflyer"
date: "2012-09-18T17:33:02Z"
tags: []
categories: ["企业架构"]
slug: "osgi"
draft: false
lastmod: "2012-09-17T16:04:43Z"
id: 1677968
url: "https://ryanflyer.iteye.com/blog/1677968"
---

1\. OSGI bundle间的协作方式

类似SOA，每个bundle可以通过BundleContext注册对外提供的服务，同时也可以通过BundleContext来获得需要引用的服务。借助使用bundle的元数据来实现工程之间package的共享。export package和import package。

在OSGI框架中，Service是个实际的概念，只有通过BundleContext注册成Service才能使得一个POJO作为Service在OSGI框架中被使用，同时也只有通过BundleContext来获取发布到框架中的Service，通过Service的方式来实现Bundle之间实例级的依赖,和Import-Package、Require-Bundle不同的地方在于通过Service获取的是其他Bundle中类的实例。

2.bundle的classloader机制

每一个bundle都有自己的classloader

![](http://dl.iteye.com/upload/attachment/0073/9419/83dd5d1f-617b-3602-9d2e-c9d22e82ffe7.jpg)

题外话：虚拟机的类加载机制

虚拟机把描述类的数据从Class文件加载到内存，并对数据进行校验、转换解析和初始化，最终形成可以被虚拟机直接使用的Java类型，这就是虚拟机的类加载机制。JVM类加载的双亲委派模型（Parents Delegation Model)。

OSGI没有采用该模型，使用了网状模型。

 

2.OSGI和Spring

使用Spring DM整合OSGI和Spring框架。

Spring-OSGI默认加载解析META-INF/spring目录下的xml文件作为spring bean配置文件.

问题1：如何把Spring中的bean发布为OSGI服务？

 

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
                      	http://www.springframework.org/schema/osgi 
                      	http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<bean id="multiplyImpl" class="org.flyer.multiply.bundle.MultiplyImpl">
	</bean>

	<osgi:service id="multiplyOsgiService" ref="multiplyImpl"
		interface="com.osgi.compute.ComputerInterface">
	</osgi:service>
</beans>
```

 

 问题2：如何引用OSGI服务？

 

 

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
                      	http://www.springframework.org/schema/osgi 
                      	http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<bean id="computeClient" class="org.flyer.compute.client.bundle.ComputeClient">
		<constructor-arg ref="computeService"></constructor-arg>
	</bean>
	<osgi:reference id="computeService" interface="com.osgi.compute.ComputeInterface"></osgi:reference>
</beans>
```

 

Spring DM 提供了spring-osgi-extender bundle，该bundle负责为你的应用bundle初始化Spring 的Application context，和Spring中的ContextLoadListener提供的功能类似。

extender bundle异步创建Application context,使用另外一个线程创建。如下图所示：

![](http://dl.iteye.com/upload/attachment/0073/9397/9f3fff6f-c407-36fe-8bd2-42a78f886309.jpg)

 

Application context的销毁：

![](http://dl.iteye.com/upload/attachment/0073/9404/06ea5657-0cf7-3837-8d84-96df0bf28ab6.jpg)  
 打包和部署基于Spring的OSGI应用

![](http://dl.iteye.com/upload/attachment/0073/9406/0009d3f3-c753-3c33-b5c3-15a41264b8a0.jpg)