{"id":10008208,"date":"2023-08-31T19:59:51","date_gmt":"2023-08-31T11:59:51","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008208"},"modified":"2023-08-31T19:59:51","modified_gmt":"2023-08-31T11:59:51","slug":"tomcat%e7%9a%84classloader%e6%89%93%e7%a0%b4%e5%8f%8c%e4%ba%b2%e5%a7%94%e6%b4%be%e6%ba%90%e7%a0%81%e8%a7%a3%e8%af%bb","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008208","title":{"rendered":"Tomcat\u7684ClassLoader\u6253\u7834\u53cc\u4eb2\u59d4\u6d3e\u6e90\u7801\u89e3\u8bfb"},"content":{"rendered":"<p>java\u52a0\u8f7d\u7c7b\u7684\u65f6\u5019\u9700\u8981\u4f7f\u7528\u7c7b\u52a0\u8f7d\u5668\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5b9a\u5236\u7c7b\u7684\u52a0\u8f7d\u5668\uff0c\u6bd4\u5982tomcat\u5c31\u6269\u5c55\u4e86\u81ea\u5df1\u7684\u7c7b\u52a0\u8f7d\u5668\u3002\u8fd9\u7bc7\u6587\u7ae0\u7ed3\u5408\u4ee3\u7801\u7814\u7a76\u4e00\u4e0bjdk\u7c7b\u7684\u52a0\u8f7d\u5668\u548ctomcat\u7684\u7c7b\u52a0\u8f7d<\/p>\n<h2>jdk\u7c7b\u7684\u52a0\u8f7d<\/h2>\n<p>\u5148\u4e0a\u56fe\u590d\u4e60\u4e00\u4e0b<br \/><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"373\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9uTc\" alt=\"image.png\" title=\"image.png\"><\/p>\n<p>\u6765\u770b\u4e00\u4e0bjdk\u7684ClassLoader\u7684\u4ee3\u7801<\/p>\n<pre><code> protected Class&lt;?&gt; loadClass(String name, boolean resolve)\n        throws ClassNotFoundException\n    {\n        synchronized (getClassLoadingLock(name)) {\n            \/\/ \u672c\u7c7b\u52a0\u8f7d\u5668\u4ece\u7f13\u5b58\u91cc\u9762\u62ff\u53d6\u5df2\u7ecf\u52a0\u8f7d\u8fc7\u7684ClassLoader\n            Class&lt;?&gt; c = findLoadedClass(name);\n            if (c == null) {\n                long t0 = System.nanoTime();\n                try {\n                    \/\/ \u7236\u7c7b\u4e3aBootStrapClassLoader\u7684classLoader \u7684parent\u53d8\u91cf\u4e3a\u7a7a\n                    if (parent != null) {\n                        c = parent.loadClass(name, false);\n                    } else {\n                        c = findBootstrapClassOrNull(name);\n                    }\n                } catch (ClassNotFoundException e) {\n                    \/\/ ClassNotFoundException thrown if class not found\n                    \/\/ from the non-null parent class loader\n                }\n\n                ..........\n    }<\/code><\/pre>\n<p>\u6ce8\u610f\u770b\u4e00\u4e0b\u6838\u5fc3\u4ee3\u7801\uff0c\u5b9e\u9645\u4e0aparent\u53ea\u8981\u4e0d\u4e3a\u7a7a\u5c31\u7531parent\u6765\u52a0\u8f7d\uff0c\u56e0\u6b64\u5f53\u7c7b\u672a\u52a0\u8f7d\u7684\u65f6\u5019\uff0c\u603b\u662f\u7531\u4e0a\u7ea7\u6765\u4f18\u5148\u52a0\u8f7d\u3002<\/p>\n<h2>tomcat\u7684\u7c7b\u52a0\u8f7d<\/h2>\n<p>\u9996\u5148\u6765\u770b\u4e00\u4e0btomcat\u7684\u7c7b\u52a0\u8f7d\u5668\uff0ctomcat\u5199\u4e86\u4e00\u4e2a\u81ea\u5df1\u7684\u7c7b\u52a0\u8f7d\u5668\uff0c\u770b\u4e0b\u57fa\u7c7bWebappClassLoaderBase\u7684\u5b9e\u73b0<\/p>\n<pre><code>@Override\n    public Class&lt;?&gt; loadClass(String name, boolean resolve) throws ClassNotFoundException {\n\n        synchronized (getClassLoadingLock(name)) {\n           ................\n            \/\/ \u4ece\u7f13\u5b58\u91cc\u9762\u53d6\uff0c\u4e0d\u5f71\u54cd\u52a0\u8f7d\u987a\u5e8f\uff0c\u8bf7\u7740\u91cd\u5ffd\u7565\n            clazz = findLoadedClass0(name);\n            ..........\n\n          \n            clazz = findLoadedClass(name);\n            .........\n\n           \/**\n     * The bootstrap class loader used to load the JavaSE classes. In some\n     * implementations this class loader is always &lt;code&gt;null&lt;\/code&gt; and in\n     * those cases {@link ClassLoader#getParent()} will be called recursively on\n     * the system class loader and the last non-null result used.\n     *\/\n            \/\/ \u6ce8\u610f\u770b\u6ce8\u91ca\uff0cjavaseLoader\u662fbootstrap\u52a0\u8f7d\u7684\uff0c\u6240\u4ee5javaseLoader\u662f\u6269\u5c55\u7c7b\u52a0\u8f7d\u5668\n            ClassLoader javaseLoader = getJavaseClassLoader();\n           \n            \n            clazz = javaseLoader.loadClass(name);\n            \n\n            boolean delegateLoad = delegate || filter(name, true);\n            \n            \/\/ (2) Search local repositories\n            if (log.isDebugEnabled()) {\n                log.debug(&quot;  Searching local repositories&quot;);\n            }\n            try {\n            \/\/ \u91cd\u5199\u4e86findClass\u65b9\u6cd5\uff0c\u4ece\u9879\u76ee\u4e2d\u7684lib\u548cclass\u91cc\u52a0\u8f7d\n                clazz = findClass(name);\n                if (clazz != null) {\n                    if (log.isDebugEnabled()) {\n                        log.debug(&quot;  Loading class from local repository&quot;);\n                    }\n                    if (resolve) {\n                        resolveClass(clazz);\n                    }\n                    return clazz;\n                }\n            } catch (ClassNotFoundException e) {\n                \/\/ Ignore\n            }\n\n            \/\/ (3) Delegate to parent unconditionally\n            if (!delegateLoad) {\n                if (log.isDebugEnabled()) {\n                    log.debug(&quot;  Delegating to parent classloader at end: &quot; + parent);\n                }\n                try {\n\/\/  \u7ee7\u627f\u4e8eSystemClassLoader\u8be5\u7c7b\uff0c\u6240\u4ee5\u8fd9\u4e2aparent\u662fSystemClassLoader\uff0c\n                    clazz = Class.forName(name, false, parent);\n                    if (clazz != null) {\n                        if (log.isDebugEnabled()) {\n                            log.debug(&quot;  Loading class from parent&quot;);\n                        }\n                        if (resolve) {\n                            resolveClass(clazz);\n                        }\n                        return clazz;\n                    }\n                } catch (ClassNotFoundException e) {\n                    \/\/ Ignore\n                }\n            }\n        }\n\n        throw new ClassNotFoundException(name);\n    }\n<\/code><\/pre>\n<p>\u628a\u987a\u5e8f\u76f8\u5173\u5916\u7684\u4ee3\u7801\u7701\u7565\u4e86\uff0c\u6ce8\u610f\u770b\u6ce8\u91ca\u3002<br \/>\u56e0\u6b64\u6765\u68b3\u7406\u4e00\u4e0b\u52a0\u8f7d\u987a\u5e8f<br \/>1.\u627eext\u52a0\u8f7d\u5668\uff0cext\u4f1a\u4f18\u5148\u8d70BootStrap\u52a0\u8f7d\u5668\uff0c\u52a0\u8f7djava\u6838\u5fc3\u7c7b\u5e93<br \/>2.ext\u52a0\u8f7d\u4e0d\u5230\u5c31\u53bb\u8d70\u91cd\u5199\u7684findClass\uff0c\u8d70web\u9879\u76ee\u7684lib\u5305\u548cclass\u76ee\u5f55\uff0c\u52a0\u8f7d\u9879\u76ee\u7684class<br \/>3.\u91cd\u5199\u7684findClass\u627e\u4e0d\u5230\uff0c\u5c31\u53bb\u7cfb\u7edf\u7c7b\u52a0\u8f7d\u5668SystemClassLoader\u6765\u52a0\u8f7d\u3002<\/p>\n<h3>tomcat\u4e3a\u4ec0\u4e48\u8981\u91cd\u5199\u8fd9\u4e2aClassLoader\u5462<\/h3>\n<p>1.\u6700\u91cd\u8981\u7684\u5c31\u662f\u505a\u9694\u79bb\u6027\uff0c\u4e0d\u540c\u7684 web\u5305\u5f15\u5165\u4e0d\u540c\u7684jar\u5305\u7248\u672c\uff0c\u5982\u679c\u91cd\u5199\u7c7b\u7684\u52a0\u8f7d\u987a\u5e8f\uff0c\u7edf\u4e00\u8d70SystemClassloader\u52a0\u8f7d\uff0c\u90a3\u4e48\u53ea\u4f1a\u52a0\u8f7d\u4e00\u4e2a\u7c7b\u5728\u5185\u5b58\uff0c\u53e6\u5916\u4e00\u4e2a\u7248\u672c\u7684class\u6c38\u8fdc\u4e5f\u4e0d\u4f1a\u52a0\u8f7d\u8fdb\u5185\u5b58\u3002\u65e0\u6cd5\u5b9e\u73b0web\u9879\u76ee\u7684\u9694\u79bb<br \/>2.\u53ef\u4ee5\u9488\u5bf9\u4e00\u4e2a\u5e94\u7528\u8fdb\u884c\u91cd\u65b0\u90e8\u7f72\uff0c\u4e00\u4e2a\u5e94\u7528\u4e00\u4e2a\u52a0\u8f7d\u5668\uff0c\u9700\u8981\u91cd\u65b0\u90e8\u7f72\uff0c\u5c31\u628a\u8fd9\u4e2aclassLoader\u52a0\u8f7d\u7684\u7c7b\u91cd\u65b0\u52a0\u8f7d\u5c31\u53ef\u4ee5\u4e86<\/p>\n<p>\u6240\u4ee5\u770b\u8d77\u6765\u6253\u7834\u53cc\u4eb2\u59d4\u6d3e\u8fd9\u79cd\u5927\u540d\u5934\u7684\u77e5\u8bc6\uff0c\u770b\u4e00\u4e0b\u6e90\u7801\u5c31\u6e05\u6670\u4e86\uff0c\u517b\u6210\u9605\u8bfb\u6e90\u7801\u4e60\u60ef\u5c31\u53ef\u4ee5<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044170598\">https:\/\/segmentfault.com\/a\/1190000044170598<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>java\u52a0\u8f7d\u7c7b\u7684\u65f6\u5019\u9700\u8981\u4f7f\u7528\u7c7b\u52a0\u8f7d\u5668\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5b9a\u5236\u7c7b\u7684\u52a0\u8f7d\u5668\uff0c\u6bd4\u5982tomcat\u5c31\u6269\u5c55\u4e86\u81ea\u5df1\u7684\u7c7b\u52a0\u8f7d\u5668\u3002\u8fd9\u7bc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008208","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008208"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008208\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}