自定义类加载器
  除了上面说的三种默认的类加载器,用户可以通过继承ClassLoader类来创建自定义的类加载器,之所以需要自定义类加载器是因为有时候我们需要通过一些特殊的途径创建类,比如网络。
  至于自定义类加载器是如何发挥作用的,ClassLoader类的loadClass方法已经把算法定义了:
  protected synchronized Class<?> loadClass(String name, boolean resolve)
  throws ClassNotFoundException
  {
  // First, check if the class has already been loaded
  Class c = findLoadedClass(name);
  if (c == null) {
  try {
  if (parent != null) {
  c = parent.loadClass(name, false);
  } else {
  c = findBootstrapClassOrNull(name);
  }
  } catch (ClassNotFoundException e) {
  // ClassNotFoundException thrown if class not found
  // from the non-null parent class loader
  }
  if (c == null) {
  // If still not found, then invoke findClass in order
  // to find the class.
  c = findClass(name);
  }
  }
  if (resolve) {
  resolveClass(c);
  }
  return c;
  }
  >1. Invoke findLoadedClass(String) to check if the class has already been loaded.
  >2. Invoke the loadClass method on the parent class loader. If the parent is null the class loader built-in to the virtual machine is used, instead.
  >3. Invoke the findClass(String) method to find the class.
  看上面的Javadoc可以知道,自定义的类加载器只要重载findClass好了。
  Context ClassLoader
  首先Java中ClassLoader上面提到的四种,Bootstrap ClassLoader,Ext ClassLoader,System ClassLoader以及用户自定义的,所以Context ClassLoader并不是一种新的类加载器,肯定是这四种的一种。
  首先关于类的加载补充一点是如果类A是被一个加载器加载的,那么类A中引用的B也是由这个加载器加载的(如果B还没有被加载的话),通常情况下是类B必须在类A的classpath下。
  但是考虑多线程环境下不同的对象可能是由不同的ClassLoader加载的,那么当一个由ClassLoaderC加载的对象A从一个线程被传到另一个线程ThreadB中,而ThreadB是由ClassLoaderD加载的,这时候如果A想获取除了自己的classpath以外的资源的话,它可以通过Thread.currentThread().getContextClassLoader()来获取线程上下文的ClassLoader了,一般是ClassLoaderD了,可以通过Thread.currentThread().setContextClassLoader(ClassLoader)来显示的设置。
  为什么要有Contex ClassLoader
  之所以有Context ClassLoader是因为Java的这种“双亲委托”机制是有局限性的:
  举网上的一个例子:
  > JNDI为例,JNDI的类是由bootstrap ClassLoader从rt.jar中间载入的,但是JNDI具体的核心驱动是由正式的实现提供的,并且通常会处于-cp参数之下(注:也是默认的System ClassLoader管理),这要求bootstartp ClassLoader去载入只有SystemClassLoader可见的类,正常的逻辑没办法处理。怎么办呢?parent可以通过获得当前调用Thread的方法获得调用线程的>Context ClassLoder 来载入类。
  我上面提到的加载资源的例子。
  Contex ClassLoader提供了一个突破这种机制的后门。
  Context ClassLoader一般在一些框架代码中用的比较多,平时写代码的时候用类的ClassLoader可以了。