为了实现这种方式,我们需要创建两个不同类型的类加载器。每一个ComponentClassLoader需要扩展Java的URLClassLoader来提供需要的逻辑来从一个JAR中获取.class文件。当然他也会执行两个其他的任务。在创建的时候,他会获取JAR清单文件并定位一个新属性Restricted-Class-Path。不像Sun提供的Class-Path属性,这个属性暗示特定的JAR应该只对这个组件有效。 public class ComponentClassLoader extends URLClassLoader { // ... public ComponentClassLoader (MasterClassLoader master, File file) { // ... JarFile jar = new JarFile(file); Manifest man = jar.getManifest(); Attributes attr = man.getMainAttributes(); List l = new ArrayList(); String str = attr.getValue("Restricted-Class-Path"); if (str != null) { StringTokenizer tok = new StringTokenizer(str); while (tok.hasMoreTokens()) { l.add(new File(file.getParentFile(), tok.nextToken()); } } this.dependencies = l; } public Class loadClass (String name, boolean resolve) throws ClassNotFoundException { try { // Try to load the class from our JAR. return loadClassForComponent(name, resolve); } catch (ClassNotFoundException ex) {} // Couldn't find it -- let the master look for it // in another components. return master.loadClassForComponent(name, resolve, dependencies); } public Class loadClassForComponent (String name, boolean resolve) throws ClassNotFoundException { C