{"id":2935,"date":"2022-04-24T00:41:53","date_gmt":"2022-04-23T16:41:53","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2935"},"modified":"2022-04-24T00:42:31","modified_gmt":"2022-04-23T16:42:31","slug":"%e6%89%93%e7%a0%b4%e7%b1%bb%e5%8a%a0%e8%bd%bd%e5%8f%8c%e4%ba%b2%e5%a7%94%e6%b4%be%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2935","title":{"rendered":"\u6253\u7834\u7c7b\u52a0\u8f7d\u53cc\u4eb2\u59d4\u6d3e\u673a\u5236"},"content":{"rendered":"\n<p><a href=\"https:\/\/blog.csdn.net\/qq_28282111\/article\/details\/121193104\">(26\u6761\u6d88\u606f) java\u7f16\u7a0b\u4e4b\u81ea\u5b9a\u4e49\u7c7b\u52a0\u8f7d\u5668\u4ee5\u53ca\u6253\u7834\u7c7b\u52a0\u8f7d\u53cc\u4eb2\u59d4\u6d3e\u7684\u65b9\u6cd5_\u5929\u7801\u884c\u7a7a\u7801\u98de\u821e\u7684\u535a\u5ba2-CSDN\u535a\u5ba2_\u81ea\u5b9a\u4e49\u7c7b\u52a0\u8f7d\u5668,\u5982\u4f55\u6253\u7834\u53cc\u4eb2\u59d4\u6d3e<\/a><\/p>\n\n\n\n<p>1.\u5982\u4f55\u81ea\u5b9a\u4e49\u7c7b\u52a0\u8f7d\u5668<\/p>\n\n\n\n<p>\u6211\u4eec\u5c31\u77e5\u9053\u5173\u952e\u7684\u65b9\u6cd5\u662ffindClass\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\u90a3\u6211\u4eec\u53ea\u8981\u7ee7\u627fClassLoader\uff0c\u91cd\u5199findClass\u65b9\u6cd5<\/mark>\u5c31\u597d\u4e86\uff08\u91cc\u9762\u8c03\u7528\u4e86defineClass\u65b9\u6cd5\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.loman.classloader;\n \nimport com.loman.classloader.Test;\n \nimport java.io.ByteArrayOutputStream;\nimport java.io.File;\nimport java.io.FileInputStream;\n \n \npublic class CustomClassLoader extends ClassLoader {\n    \n    @Override\n    protected Class&lt;?> findClass(String name) throws ClassNotFoundException {\n        File f = new File(\"d:\/test\/\", name.replace(\".\", \"\/\").concat(\".class\"));\n        FileInputStream fis = null;\n        ByteArrayOutputStream baos = null;\n        try {\n            fis = new FileInputStream(f);\n            baos = new ByteArrayOutputStream();\n            int b = 0;\n \n            while ((b=fis.read()) !=0) {\n                baos.write(b);\n            }\n \n            byte&#91;] bytes = baos.toByteArray();            \n \n            return defineClass(name, bytes, 0, bytes.length);\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            if(baos != null) {\n                baos.close();\n            }            \n            if(fis != null) {\n                fis.close();\n            }\n            \n        }\n        return super.findClass(name); \/\/throws ClassNotFoundException\n    }\n \n    \n    public static void main(String&#91;] args) throws Exception {\n        ClassLoader l = new CustomClassLoader();\n        Class clazz = l.loadClass(\"com.loman.classloader.Test\");\n        Class clazz1 = l.loadClass(\"com.loman.classloader.Test\");\n \n        System.out.println(clazz == clazz1);\n \n        Test h = (Test)clazz.newInstance();   \/\/Test\u662f\u81ea\u5df1\u5b9a\u4e49\u7684\u4e00\u4e2a\u7c7b\uff0c\u4ee3\u7801\u5c31\u4e0d\u8d34\u4e86\n        h.method1();    \/\/Test\u7c7b\u7684\u5b9e\u4f8b\u65b9\u6cd5                            \n \n        System.out.println(l.getClass().getClassLoader());\n        System.out.println(l.getParent());\n \n        System.out.println(getSystemClassLoader());\n    }\n}<\/code><\/pre>\n\n\n\n<p>2.\u6253\u7834\u53cc\u4eb2\u59d4\u6d3e<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"968\" height=\"477\" src=\"https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2022\/04\/image-1.png\" alt=\"\" class=\"wp-image-2942\" srcset=\"https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2022\/04\/image-1.png 968w, https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2022\/04\/image-1-300x148.png 300w, https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2022\/04\/image-1-768x378.png 768w\" sizes=\"(max-width: 968px) 100vw, 968px\" \/><\/figure>\n\n\n\n<p>\u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u53cc\u4eb2\u59d4\u6d3e\u662f\u4e00\u4e2a\u81ea\u5e95\u5411\u4e0a\u9010\u5c42\u68c0\u67e5\u7c7b\u662f\u5426\u52a0\u8f7d\uff0c\u81ea\u9876\u5411\u4e0b\u9010\u5c42\u8fdb\u884c\u5b9e\u9645\u67e5\u627e\u548c\u52a0\u8f7d\u7684\u8fc7\u7a0b\uff0c\u6709\u70b9\u9012\u5f52\u7684\u610f\u601d\u3002\u68c0\u67e5\u7684\u8fc7\u7a0b\u5c31\u662f\u52a0\u8f7d\u4e4b\u524d\uff0c\u5148\u68c0\u67e5\u4e00\u4e0b\u662f\u4e0d\u662f\u5df2\u7ecf\u52a0\u8f7d\u8fc7\u4e86\uff0c\u5982\u679c\u81ea\u5df1\u52a0\u8f7d\u4e86\u90a3\u5c31\u8fd4\u56de\u7ed3\u679c\uff0c\u5982\u679c\u81ea\u5df1\u6ca1\u52a0\u8f7d\uff0c\u90a3\u5c31\u95ee\u95ee\u7236\u52a0\u8f7d\u5668\uff08\u6ce8\u610f\u4e0d\u662f\u7c7b\u52a0\u8f7d\u5668\u7684\u52a0\u8f7d\u5668\uff0c\u4e5f\u4e0d\u662f\u7c7b\u52a0\u8f7d\u5668\u7684\u7236\u7c7b\u52a0\u8f7d\u5668\uff09\u662f\u5426\u52a0\u8f7d\u4e86\uff0c\u5982\u679c\u7236\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u4e86\u5c31\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\uff0c\u5982\u679c\u6ca1\u6709\u5c31\u7ee7\u7eed\u5f80\u4e0a\u4e00\u5c42\u53bb\u68c0\u67e5\u3002\u90a3\u5982\u679c\u5230\u4e86\u6700\u9876\u5c42\u7684\u7c7b\u52a0\u8f7d\u5668\u8fd8\u662f\u6ca1\u6709\u52a0\u8f7d\u7684\u8bdd\uff0c\u90a3\u5c31\u201c\u8c03\u5934\u201d\uff0c\u9010\u5c42\u5f80\u4e0b\u53bb\u67e5\u627e\uff0c\u5982\u679c\u80fd\u627e\u5230\uff08\u7c7b\u52a0\u8f7d\u5668\u6709\u5404\u81ea\u7684\u201c\u8d23\u4efb\u8303\u56f4\u201d\uff09\u90a3\u5c31\u52a0\u8f7d\uff0c\u7136\u540e\u8fd4\u56de\u7ed3\u679c\uff0c\u5982\u679c\u627e\u4e0d\u5230\uff0c\u90a3\u5c31\u4ea4\u7ed9\u4e0b\u4e00\u5c42\uff0c\u8fd9\u6837\u5c42\u5c42\u9012\u63a8\u3002\u5982\u679c\u5230\u6700\u5e95\u5c42\u8fd8\u662f\u627e\u4e0d\u5230\uff0c\u600e\u4e48\u529e\uff1f\u8fd9\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\uff1aClassNotFoundException\uff0c\u8fd9\u4e2a\u5f02\u5e38\u76f8\u4fe1\u5927\u5bb6\u90fd\u4e0d\u964c\u751f\uff0c\u90a3\u53ef\u80fd\u7684\u539f\u56e0\u5c31\u662f.class\u6587\u4ef6\u7684\u8def\u5f84\u5199\u9519\u4e86\uff0c\u6216\u8005\u6587\u4ef6\u4e0d\u5b58\u5728\u3002<\/p>\n\n\n\n<p>\u53cc\u4eb2\u59d4\u6d3e\u7684\u76ee\u7684\u4e3b\u8981\u662f\u4e3a\u4e86\u5b89\u5168\uff0c\u5176\u6b21\u5c31\u662f\u907f\u514d\u91cd\u590d\u52a0\u8f7d\u3002<\/p>\n\n\n\n<p>\u65e2\u7136\u6211\u4eec\u77e5\u9053\u4e86\u53cc\u4eb2\u59d4\u6d3e\u7684\u539f\u7406\u6d41\u7a0b\uff0c\u90a3\u8981\u600e\u4e48\u53bb\u6253\u7834\u5b83\u5462\uff1f\u901a\u8fc7\u5206\u6790jdk\u6e90\u7801\uff0c\u53ef\u4ee5\u5f97\u5230\u7b54\u6848\uff0c\u90a3\u5c31\u662f\u5728<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\u81ea\u5b9a\u4e49\u7c7b\u52a0\u8f7d\u5668\u7684\u57fa\u7840\u4e0a\uff0c\u91cd\u5199loadClass\u65b9\u6cd5\u3002\u8fd9\u91cc\u6211\u5c31\u53ea\u8d34\u91cd\u5199\u7684loadClass\u65b9\u6cd5<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic Class&lt;?> loadClass(String name) throws ClassNotFoundException {\n \n    File f = new File(\"D:\/test\/\" + name.replace(\".\", \"\/\").concat(\".class\"));\n \n    if(!f.exists()) return super.loadClass(name);\n \n    try {\n \n        InputStream is = new FileInputStream(f);\n \n        byte&#91;] b = new byte&#91;is.available()];\n        is.read(b);\n        return defineClass(name, b, 0, b.length);\n    } catch (IOException e) {\n        e.printStackTrace();\n    }\n \n    return super.loadClass(name);\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.csdn.net\/tags\/MtTaEgwsOTk5MTQ2LWJsb2cO0O0O.html\">(26\u6761\u6d88\u606f) \u4e3a\u4ec0\u4e48tomcat\u8981\u6253\u7834\u7c7b\u52a0\u8f7d\u6982\u5ff5 &#8211; CSDN<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Tomcat\u7c7b\u52a0\u8f7d\u673a\u5236\u8981\u8003\u8651\u7684\u95ee\u9898<\/h2>\n\n\n\n<p>Tomcat\u4f5c\u4e3aServlet\u5bb9\u5668\uff0c\u5b83\u8d1f\u8d23\u52a0\u8f7d\u6211\u4eec\u7684Servlet\u7c7b\uff0c\u6b64\u5916\u5b83\u8fd8\u8d1f\u8d23\u52a0\u8f7dServlet\u6240\u4f9d\u8d56\u7684 JAR \u5305\u3002\u5e76\u4e14Tomcat\u672c\u8eab\u4e5f\u662f\u4e5f\u662f\u4e00\u4e2aJava\u7a0b\u5e8f\uff0c\u56e0\u6b64\u5b83\u9700\u8981\u52a0\u8f7d\u81ea\u5df1\u7684\u7c7b\u548c\u4f9d\u8d56\u7684JAR\u5305\uff0c\u6240\u4ee5\u53ef\u80fd\u8981\u8003\u8651\u8fd9\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n\n\n\n<ol><li>\u5047\u5982\u5728Tomcat\u4e2d\u8fd0\u884c\u4e86\u4e24\u4e2aWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u4e24\u4e2aWeb\u5e94\u7528\u4e2d\u6709\u540c\u540d\u7684 Servlet\uff0c\u4f46\u662f\u529f\u80fd\u4e0d\u540c\uff0cTomcat\u9700\u8981\u540c\u65f6\u52a0\u8f7d\u548c\u7ba1\u7406\u8fd9\u4e24\u4e2a\u540c\u540d\u7684Servlet \u7c7b\uff0c\u4fdd\u8bc1\u5b83\u4eec\u4e0d\u4f1a\u51b2\u7a81\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cWeb\u5e94\u7528\u4e4b\u95f4\u7684\u7c7b\u9700\u8981\u9694\u79bb<\/li><li>\u5047\u5982\u4e24\u4e2aWeb\u5e94\u7528\u90fd\u4f9d\u8d56\u540c\u4e00\u4e2a\u7b2c\u4e09\u65b9\u7684JAR\u5305\uff0c\u6bd4\u5982Spring\uff0c\u90a3Spring\u7684JAR\u5305\u88ab\u52a0\u8f7d\u5230\u5185\u5b58\u540e\uff0cTomcat\u8981\u4fdd\u8bc1\u8fd9\u4e24\u4e2aWeb\u5e94\u7528\u80fd\u591f\u5171\u4eab\uff0c\u4e5f\u5c31\u662f\u8bf4Spring\u7684JAR\u5305\u53ea\u88ab\u52a0\u8f7d\u4e00\u6b21\uff0c\u5426\u5219\u968f\u7740\u4f9d\u8d56\u7684\u7b2c\u4e09\u65b9JAR\u5305\u589e\u591a\uff0cJVM \u7684\u5185\u5b58\u4f1a\u81a8\u80c0<\/li><li>Tomcat\u81ea\u8eab\u4e5f\u662f\u4e00\u4e2aJava\u7a0b\u5e8f\uff0c\u9700\u8981\u9694\u79bbTomcat\u672c\u8eab\u7684\u7c7b\u548cWeb\u5e94\u7528\u7684\u7c7b\uff0c\u907f\u514d\u76f8\u4e92\u5f71\u54cd\uff0c\u6bd4\u5982Web\u5e94\u7528\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540c\u540d\u7c7b\u5bfc\u81f4Tomcat\u672c\u8eab\u7684\u7c7b\u65e0\u6cd5\u52a0\u8f7d<\/li><\/ol>\n\n\n\n<p>\u6240\u4ee5\uff0cTomcat\u662f\u5982\u4f55\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\uff1f\u7b54\u6848\u662f\u901a\u8fc7\u8bbe\u8ba1\u591a\u5c42\u6b21\u7684\u7c7b\u52a0\u8f7d\u5668\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/cdn.lidol.top\/lidol_blog\/tomcat_classLoader.png\"><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/15597971411e99712caa40adf2ca18fc.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p>\u70ed\u52a0\u8f7d\u3001osgi\u7b49\u90fd\u6253\u7834\u4e86\u53cc\u4eb2\u59d4\u6d3e\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>(26\u6761\u6d88\u606f) java\u7f16\u7a0b\u4e4b\u81ea\u5b9a\u4e49\u7c7b\u52a0\u8f7d\u5668\u4ee5\u53ca\u6253\u7834\u7c7b\u52a0\u8f7d\u53cc\u4eb2\u59d4\u6d3e\u7684\u65b9\u6cd5_\u5929\u7801\u884c\u7a7a\u7801\u98de\u821e\u7684\u535a\u5ba2-CSDN\u535a\u5ba2_ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2935"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2935"}],"version-history":[{"count":15,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2935\/revisions"}],"predecessor-version":[{"id":2951,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2935\/revisions\/2951"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}