首页  

URLClassLoader简介     所属分类 java 浏览量 1232
类加载双亲委派 
防止多次加载,防止加载冲突



Bootstrap classLoader   $JAVA_HOME/jre/lib/rt.jar
Bootstrap ClassLoader 不继承 ClassLoader

ClassLoader SecureClassLoader URLClassLoader 

ExtClassLoader  $JAVA_HOME/jre/lib/ext
AppClassLoader  系统类加载器 ClassLoader.getSystemClassLoader()  加载 classpath 里的类

ExtClassLoader AppClassLoader 都继承 URLClassLoader

ExtClassLoader 父加载器为 null

双亲委派机制  ClassLoader loadClass 方法

先检查是否已经被加载过,若没有加载则调用父类加载器的loadClass()方法,若父类加载器为空则默认使用启动类加载器作为父类加载器。
如果父类加载器加载失败 ClassNotFoundException ,调用自己的findClass()方法进行加载。

URLClassLoader 实现了 findClass  在指定的路径加载 类 
public URLClassLoader(URL[] urls)

This class loader is used to load classes and resources 
from a search path of URLs referring to both JAR files and directories. 
Any URL that ends with a '/' is assumed to refer to a directory. 
Otherwise, the URL is assumed to refer to a JAR file which will be opened as needed.
 

  protected Class<?> findClass(final String name)
        throws ClassNotFoundException
    {
        final Class<?> result;
        try {
            result = AccessController.doPrivileged(
                new PrivilegedExceptionAction<Class<?>>() {
                    public Class<?> run() throws ClassNotFoundException {
                        String path = name.replace('.', '/').concat(".class");
                        Resource res = ucp.getResource(path, false);
                        if (res != null) {
                            try {
                                return defineClass(name, res);
                            } catch (IOException e) {
                                throw new ClassNotFoundException(name, e);
                            }
                        } else {
                            return null;
                        }
                    }
                }, acc);
        } catch (java.security.PrivilegedActionException pae) {
            throw (ClassNotFoundException) pae.getException();
        }
        if (result == null) {
            throw new ClassNotFoundException(name);
        }
        return result;
    }


URLClassPath
String path = name.replace('.', '/').concat(".class");
Resource res = ucp.getResource(path, false);

defineClass(name, res)

public URL findResource(final String name)
public Enumeration findResources(final String name)


ucp.findResource(name, true);

URLClassPath
This class is used to maintain a search path of URLs for loading classes and resources from both JAR files and directories.
getResource 与 findResource 区别 
返回类型不一样




sun.misc.URLClassPath.findResource(String arg0, boolean arg1)

http://www.docjar.com/html/api/sun/misc/URLClassPath.java.html

         Finds the resource with the specified name on the URL search path or null if not found or security check fails.
         name      the name of the resource
         check     whether to perform a security check
         return a URL for the resource, or null if the resource could not be found.
          
         public URL findResource(String name, boolean check) {
             Loader loader;
             for (int i = 0; (loader = getLoader(i)) != null; i++) {
                URL url = loader.findResource(name, check);
                if (url != null) {
                     return url;
                 }
             }
             return null;
         }
  
  
FileLoader   JarLoader

private Loader getLoader(final URL url) throws IOException {
                     
                        
                       String file = url.getFile();
                       if (file != null && file.endsWith("/")) {
                           if ("file".equals(url.getProtocol())) {
                               return new FileLoader(url);
                           } else {
                               return new Loader(url);
                           }
                        } else {
                           return new JarLoader(url, jarHandler, lmap);
                        }

上一篇     下一篇
java restful 框架

类加载器中findClass与loadClass的区别

jetty嵌入式使用

netty配置参数

netty参数及相关代码

netty4 echo例子 日志记录