{"id":336,"date":"2023-02-25T06:48:50","date_gmt":"2023-02-24T22:48:50","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=336"},"modified":"2023-04-30T14:49:57","modified_gmt":"2023-04-30T06:49:57","slug":"java-parental-delegation-mechanism","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/java-parental-delegation-mechanism\/","title":{"rendered":"Java\u4e4b\u53cc\u4eb2\u59d4\u6258\u673a\u5236"},"content":{"rendered":"<p>\u53cc\u4eb2\u59d4\u6d3e\u673a\u5236\u7684\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/p>\n<p>\uff081\uff09\u5f53\u524d<code>ClassLoader<\/code>\u9996\u5148\u4ece\u81ea\u5df1\u5df2\u7ecf\u52a0\u8f7d\u7684\u7c7b\u4e2d\u67e5\u8be2\u662f\u5426\u6b64\u7c7b\u5df2\u7ecf\u52a0\u8f7d\uff0c\u5982\u679c\u5df2\u7ecf\u52a0\u8f7d\u5219\u76f4\u63a5\u8fd4\u56de\u539f\u6765\u5df2\u7ecf\u52a0\u8f7d\u7684\u7c7b\u3002\u6bcf\u4e2a\u7c7b\u52a0\u8f7d\u5668\u90fd\u6709\u81ea\u5df1\u7684\u52a0\u8f7d\u7f13\u5b58\uff0c\u5f53\u4e00\u4e2a\u7c7b\u88ab\u52a0\u8f7d\u4e86\u4ee5\u540e\u5c31\u4f1a\u653e\u5165\u7f13\u5b58\uff0c\u7b49\u4e0b\u6b21\u52a0\u8f7d\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u4e86\u3002<\/p>\n<p>\uff082\uff09\u5f53\u524d<code>ClassLoader<\/code>\u7684\u7f13\u5b58\u4e2d\u6ca1\u6709\u627e\u5230\u88ab\u52a0\u8f7d\u7684\u7c7b\u7684\u65f6\u5019\uff0c\u59d4\u6258\u7236\u7c7b\u52a0\u8f7d\u5668\u53bb\u52a0\u8f7d\uff0c\u4f9d\u6b21\u9012\u5f52\uff08\u5373\uff1a\u7236\u7c7b\u52a0\u8f7d\u5668\u91c7\u7528\u540c\u6837\u7684\u7b56\u7565\uff0c\u9996\u5148\u67e5\u770b\u81ea\u5df1\u7684\u7f13\u5b58\uff0c\u7136\u540e\u59d4\u6258\u7236\u7c7b\u7684\u7236\u7c7b\u53bb\u52a0\u8f7d\uff0c\u4e00\u76f4\u5230<code>Bootstrp ClassLoader<\/code>\uff09\uff0c\u5982\u679c\u7236\u7c7b\u52a0\u8f7d\u5668\u53ef\u4ee5\u5b8c\u6210\u7c7b\u52a0\u8f7d\u4efb\u52a1\uff0c\u5c31\u6210\u529f\u8fd4\u56de\u3002<\/p>\n<p>\uff083\uff09\u5f53\u6240\u6709\u7684\u7236\u7c7b\u52a0\u8f7d\u5668\u90fd\u6ca1\u6709\u52a0\u8f7d\u7684\u65f6\u5019\uff0c\u518d\u7531\u5f53\u524d\u7684\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\uff0c\u5e76\u5c06\u5176\u653e\u5165\u5b83\u81ea\u5df1\u7684\u7f13\u5b58\u4e2d\uff0c\u4ee5\u4fbf\u4e0b\u6b21\u6709\u52a0\u8f7d\u8bf7\u6c42\u7684\u65f6\u5019\u76f4\u63a5\u8fd4\u56de\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">protected Class&lt;?&gt; loadClass(String className, boolean resolve) throws  ClassNotFoundException { \n    \/\/\u9996\u5148\u4ece\u5df2\u7ecf\u52a0\u8f7d\u7684\u7c7b\u4e2d\u67e5\u627e\n    Class&lt;?&gt; clazz = findLoadedClass(className);    \n    if (clazz == null) {\n        ClassNotFoundException suppressed = null;     \n        try {   \n             \/\/\u5982\u679c\u6ca1\u6709\u52a0\u8f7d\u8fc7\uff0c\u5148\u8c03\u7528\u7236\u52a0\u8f7d\u5668\u7684loadClass\n             clazz = parent.loadClass(className, false);\n        } catch (ClassNotFoundException e) {\n             suppressed = e;\n        }\n\n        if (clazz == null) {        \n           try {           \n               \/\/\u7236\u52a0\u8f7d\u5668\u90fd\u6ca1\u6709\u52a0\u8f7d\uff0c\u5219\u5c1d\u8bd5\u52a0\u8f7d\n               clazz = findClass(className);\n           } catch (ClassNotFoundException e) {\n                e.addSuppressed(suppressed);       \n                throw e;\n           }\n        }\n    }    \n\n    return clazz;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53cc\u4eb2\u59d4\u6d3e\u673a\u5236\u7684\u5de5\u4f5c\u6d41\u7a0b\uff1a \uff081\uff09\u5f53\u524dClassLoader\u9996\u5148\u4ece\u81ea\u5df1\u5df2\u7ecf\u52a0\u8f7d\u7684\u7c7b\u4e2d\u67e5\u8be2\u662f\u5426\u6b64\u7c7b\u5df2\u7ecf\u52a0\u8f7d\uff0c\u5982\u679c\u5df2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}