如下圖所示:

對于資源和類的裝載主要包括以下三種途徑:
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。
如下圖

這么說起來,還是比較的抽象,還是從一個實例來具體的分析。
假設有如下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",

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;
}

}
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
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;
}

}
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: * ,這是比較極端的設置,一般不會推薦使用,主要破壞了相關的封裝性。
完整的類查詢如下圖:

關于類裝載機制基本就是這些,對于初學者來說,這還是會有些困惑的。