---
title: "osgi 入门 三 关于类装载机制"
author: "zyl"
date: "2009-02-17T16:53:02Z"
tags: []
slug: "osgi"
draft: false
lastmod: "2009-02-17T16:53:02Z"
id: 334326
url: "https://zyl.iteye.com/blog/334326"
---

osgi一个最大的特色就是使用不同的ClassLoader,让多个bundle共享一个虚拟机，而每一个bundle使用单独的ClassLoader。  
如下图所示：  
  ![](/upload/attachment/76225/d1aad416-2cb8-3e9e-b7be-5dc9a08d1724.gif)  
对于资源和类的装载主要包括以下三种途径：  
Boot classpath：启动类环境，包括java.\*包和相应的实现包。  
Framework classpath:osgi框架拥有一个独立的ClassLoader，用于装载框架接口类，以及相应的实现类。  
Bundle Space:每个bundle包括了与bundle相关的jar文件，以及相关的资源。

对于一个类的查询，主要通过以下途径进行查询：  
1.从Parent ClassLoader中装载  
2.从Import Package中查询类路径。  
3.从Required bundles中查询类  
4.自身bundle，相关ClassPath  
5.相关的插件片段Fragment。  
如下图  
  ![](/upload/attachment/76227/215a7e18-ee52-3051-a61b-f58979248109.gif)  
这么说起来，还是比较的抽象，还是从一个实例来具体的分析。  
假设有如下Bundle,  
相关的描述文件如下：

```
Bundle:org.zframework.core
Manifest-Version: 1.0
Bundle-Name: core
Bundle-SymbolicName: org.zframework.core;singleton:=true
Bundle-ClassPath: .
Import-Package: net.sf.ehcache;version="1.3.0",
 net.sf.ehcache.config;version="1.3.0",
 net.sf.ehcache.event;version="1.3.0",
 org.osgi.framework;version="1.4.0",
 org.osgi.service.log;version="1.3.0",
 ...
Require-Bundle: org.eclipse.equinox.common;visibility:=reexport,
 ...
Export-Package: org.zframework.core;version="1.0.0",
 ...
```

 

现在创建了如下的类：

```
package org.zframework.core;
import java.net.URL;
import java.util.Collection;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.osgi.framework.BundleContext;
public class EhCacheManager extends AbstractCacheManager implements
    BundleContextAware {
    private String cacheFile;
    private BundleContext context;
    private CacheManager manager;
    public Cache getCache(String cacheName) {
    Cache cache = manager.getCache(cacheName);
    if (cache == null) {
          ...
    }
    return cache;
    }
    ...
}
```

 

假设有一个Bundle org.zframework.model需要使用此类，描述如下：

```
Manifest-Version: 1.0
Bundle-Name: model
Bundle-SymbolicName: org.zframework.model;singleton:=true
Bundle-ClassPath: .
Import-Package: javax.persistence;version="1.0.0",
  javax.persistence.spi;version="1.0.0",
  org.osgi.framework;version="1.4.0",
  ...
 Require-Bundle: org.zframework.core

类Model
package org.zframework.model;
import java.net.URL;
import java.util.Collection;
import org.zframework.core.EhCacheManager ;
public class Model{
    private EhCacheManager manager;
    public Object getCache(String key) {
    Object o = manager.getCache(key);
    return o;
    }
    ...
}
```

 

下面简单说明一下装载过程：  
在装载Model类时，  
1.对于java.\*类的装载，由相应的Boot ClassLoader 装载  
2.装载EhCacheManager，  
       1.在Import-Package中查询是否存在匹配中的org.zframework.core包声明，如果有，则委派给org.zframework.core包声明的Bundle进行装载，本例没有，则继续。  
       2.在Require-Bundle中查询所有的Export声明包，判断是否存在org.zframework.core匹配。本例中，应该存在。如果不存在，则执行步骤2.3  
       3.查询本bundle的类路径，是否有此类存在。不存在，继续2.4步骤  
       4.查询所有的fragments bundle（特殊的bundle） 类路径。不存在，继续2.5步骤  
       5.判断是否使用DynamicImport-Package: * 声明，如果是则查询所有的bundle中声明的Export包。如果还是不存在，则抛出ClassNotFound例外。  
关于DynamicImport-Package: * ，这是比较极端的设置，一般不会推荐使用，主要破坏了相关的封装性。  
完整的类查询如下图：  
  ![](/upload/attachment/76229/630dfedd-17af-39e1-aa04-be63729a0f15.gif)  
关于类装载机制基本就是这些，对于初学者来说，这还是会有些困惑的。