JVM中的双亲委派机制及沙箱安全机制

    科技2024-08-19  30

    双亲委派机制

    工作原理:

    如果一个类加载器收到了类加载请求,它并不会自己先去加载,而是把这个请求委托给父类的加载器去执行;如果父类加载器还存在其父类加载器,则进一步向上委托,依次递归,请求最终将到达顶层的启动类加载器如果父类加载器可以完成类加载任务,就成功返回,倘若父类加载器无法完成此加载任务,子加载器才会尝试自己去加载,这就是双亲委派模 #mermaid-svg-iPaE43c15pzT2Ask .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-iPaE43c15pzT2Ask .label text{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .node rect,#mermaid-svg-iPaE43c15pzT2Ask .node circle,#mermaid-svg-iPaE43c15pzT2Ask .node ellipse,#mermaid-svg-iPaE43c15pzT2Ask .node polygon,#mermaid-svg-iPaE43c15pzT2Ask .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-iPaE43c15pzT2Ask .node .label{text-align:center;fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .node.clickable{cursor:pointer}#mermaid-svg-iPaE43c15pzT2Ask .arrowheadPath{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-iPaE43c15pzT2Ask .flowchart-link{stroke:#333;fill:none}#mermaid-svg-iPaE43c15pzT2Ask .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-iPaE43c15pzT2Ask .edgeLabel rect{opacity:0.9}#mermaid-svg-iPaE43c15pzT2Ask .edgeLabel span{color:#333}#mermaid-svg-iPaE43c15pzT2Ask .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-iPaE43c15pzT2Ask .cluster text{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-iPaE43c15pzT2Ask .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-iPaE43c15pzT2Ask text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-iPaE43c15pzT2Ask .actor-line{stroke:grey}#mermaid-svg-iPaE43c15pzT2Ask .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-iPaE43c15pzT2Ask .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-iPaE43c15pzT2Ask #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-iPaE43c15pzT2Ask .sequenceNumber{fill:#fff}#mermaid-svg-iPaE43c15pzT2Ask #sequencenumber{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask #crosshead path{fill:#333;stroke:#333}#mermaid-svg-iPaE43c15pzT2Ask .messageText{fill:#333;stroke:#333}#mermaid-svg-iPaE43c15pzT2Ask .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-iPaE43c15pzT2Ask .labelText,#mermaid-svg-iPaE43c15pzT2Ask .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-iPaE43c15pzT2Ask .loopText,#mermaid-svg-iPaE43c15pzT2Ask .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-iPaE43c15pzT2Ask .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-iPaE43c15pzT2Ask .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-iPaE43c15pzT2Ask .noteText,#mermaid-svg-iPaE43c15pzT2Ask .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-iPaE43c15pzT2Ask .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-iPaE43c15pzT2Ask .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-iPaE43c15pzT2Ask .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-iPaE43c15pzT2Ask .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask .section{stroke:none;opacity:0.2}#mermaid-svg-iPaE43c15pzT2Ask .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-iPaE43c15pzT2Ask .section2{fill:#fff400}#mermaid-svg-iPaE43c15pzT2Ask .section1,#mermaid-svg-iPaE43c15pzT2Ask .section3{fill:#fff;opacity:0.2}#mermaid-svg-iPaE43c15pzT2Ask .sectionTitle0{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .sectionTitle1{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .sectionTitle2{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .sectionTitle3{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-iPaE43c15pzT2Ask .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask .grid path{stroke-width:0}#mermaid-svg-iPaE43c15pzT2Ask .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-iPaE43c15pzT2Ask .task{stroke-width:2}#mermaid-svg-iPaE43c15pzT2Ask .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask .taskText:not([font-size]){font-size:11px}#mermaid-svg-iPaE43c15pzT2Ask .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-iPaE43c15pzT2Ask .task.clickable{cursor:pointer}#mermaid-svg-iPaE43c15pzT2Ask .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-iPaE43c15pzT2Ask .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-iPaE43c15pzT2Ask .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-iPaE43c15pzT2Ask .taskText0,#mermaid-svg-iPaE43c15pzT2Ask .taskText1,#mermaid-svg-iPaE43c15pzT2Ask .taskText2,#mermaid-svg-iPaE43c15pzT2Ask .taskText3{fill:#fff}#mermaid-svg-iPaE43c15pzT2Ask .task0,#mermaid-svg-iPaE43c15pzT2Ask .task1,#mermaid-svg-iPaE43c15pzT2Ask .task2,#mermaid-svg-iPaE43c15pzT2Ask .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-iPaE43c15pzT2Ask .taskTextOutside0,#mermaid-svg-iPaE43c15pzT2Ask .taskTextOutside2{fill:#000}#mermaid-svg-iPaE43c15pzT2Ask .taskTextOutside1,#mermaid-svg-iPaE43c15pzT2Ask .taskTextOutside3{fill:#000}#mermaid-svg-iPaE43c15pzT2Ask .active0,#mermaid-svg-iPaE43c15pzT2Ask .active1,#mermaid-svg-iPaE43c15pzT2Ask .active2,#mermaid-svg-iPaE43c15pzT2Ask .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-iPaE43c15pzT2Ask .activeText0,#mermaid-svg-iPaE43c15pzT2Ask .activeText1,#mermaid-svg-iPaE43c15pzT2Ask .activeText2,#mermaid-svg-iPaE43c15pzT2Ask .activeText3{fill:#000 !important}#mermaid-svg-iPaE43c15pzT2Ask .done0,#mermaid-svg-iPaE43c15pzT2Ask .done1,#mermaid-svg-iPaE43c15pzT2Ask .done2,#mermaid-svg-iPaE43c15pzT2Ask .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-iPaE43c15pzT2Ask .doneText0,#mermaid-svg-iPaE43c15pzT2Ask .doneText1,#mermaid-svg-iPaE43c15pzT2Ask .doneText2,#mermaid-svg-iPaE43c15pzT2Ask .doneText3{fill:#000 !important}#mermaid-svg-iPaE43c15pzT2Ask .crit0,#mermaid-svg-iPaE43c15pzT2Ask .crit1,#mermaid-svg-iPaE43c15pzT2Ask .crit2,#mermaid-svg-iPaE43c15pzT2Ask .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-iPaE43c15pzT2Ask .activeCrit0,#mermaid-svg-iPaE43c15pzT2Ask .activeCrit1,#mermaid-svg-iPaE43c15pzT2Ask .activeCrit2,#mermaid-svg-iPaE43c15pzT2Ask .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-iPaE43c15pzT2Ask .doneCrit0,#mermaid-svg-iPaE43c15pzT2Ask .doneCrit1,#mermaid-svg-iPaE43c15pzT2Ask .doneCrit2,#mermaid-svg-iPaE43c15pzT2Ask .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-iPaE43c15pzT2Ask .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-iPaE43c15pzT2Ask .milestoneText{font-style:italic}#mermaid-svg-iPaE43c15pzT2Ask .doneCritText0,#mermaid-svg-iPaE43c15pzT2Ask .doneCritText1,#mermaid-svg-iPaE43c15pzT2Ask .doneCritText2,#mermaid-svg-iPaE43c15pzT2Ask .doneCritText3{fill:#000 !important}#mermaid-svg-iPaE43c15pzT2Ask .activeCritText0,#mermaid-svg-iPaE43c15pzT2Ask .activeCritText1,#mermaid-svg-iPaE43c15pzT2Ask .activeCritText2,#mermaid-svg-iPaE43c15pzT2Ask .activeCritText3{fill:#000 !important}#mermaid-svg-iPaE43c15pzT2Ask .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-iPaE43c15pzT2Ask g.classGroup text .title{font-weight:bolder}#mermaid-svg-iPaE43c15pzT2Ask g.clickable{cursor:pointer}#mermaid-svg-iPaE43c15pzT2Ask g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-iPaE43c15pzT2Ask g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-iPaE43c15pzT2Ask .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-iPaE43c15pzT2Ask .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-iPaE43c15pzT2Ask .dashed-line{stroke-dasharray:3}#mermaid-svg-iPaE43c15pzT2Ask #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask .commit-id,#mermaid-svg-iPaE43c15pzT2Ask .commit-msg,#mermaid-svg-iPaE43c15pzT2Ask .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-iPaE43c15pzT2Ask g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-iPaE43c15pzT2Ask g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-iPaE43c15pzT2Ask g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-iPaE43c15pzT2Ask .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-iPaE43c15pzT2Ask .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-iPaE43c15pzT2Ask .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-iPaE43c15pzT2Ask .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-iPaE43c15pzT2Ask .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-iPaE43c15pzT2Ask .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-iPaE43c15pzT2Ask .edgeLabel text{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iPaE43c15pzT2Ask .node circle.state-start{fill:black;stroke:black}#mermaid-svg-iPaE43c15pzT2Ask .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-iPaE43c15pzT2Ask #statediagram-barbEnd{fill:#9370db}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-state .divider{stroke:#9370db}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-iPaE43c15pzT2Ask .note-edge{stroke-dasharray:5}#mermaid-svg-iPaE43c15pzT2Ask .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-iPaE43c15pzT2Ask .error-icon{fill:#522}#mermaid-svg-iPaE43c15pzT2Ask .error-text{fill:#522;stroke:#522}#mermaid-svg-iPaE43c15pzT2Ask .edge-thickness-normal{stroke-width:2px}#mermaid-svg-iPaE43c15pzT2Ask .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-iPaE43c15pzT2Ask .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-iPaE43c15pzT2Ask .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-iPaE43c15pzT2Ask .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-iPaE43c15pzT2Ask .marker{fill:#333}#mermaid-svg-iPaE43c15pzT2Ask .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-iPaE43c15pzT2Ask { color: rgba(0, 0, 0, 0.75); font: ; } 向上委托 向上委托 扩展类加载器 Extension ClassLoader 引导类加载器 Bootstrap ClassLoader 系统类加载器 Application ClassLoader 自定义类加载器 Custom ClassLoader 自定义类加载器 Custom ClassLoader public class StringTest { public static void main(String[] args) { java.lang.String str = new java.lang.String(); System.out.println("hello"); } } public class String { static { System.out.println("这是自己定义String类的静态代码块"); } }

    上述代码输出hello,并没有输出静态代码块中的内容。是因为当new String的时候从系统加载器向上委托直到引导类加载器,引导类加载器能直接加载java.lang.String(核心API)。

    package java.lang; public class String { static { System.out.println("这是自己定义的String类的静态代码块"); } public static void main(String[] args) { System.out.println("这是String主方法"); } }

    上述代码会出现错误

    错误: 在类 java.lang.String 中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application

    原因是:当加载String类的时候会向上委托至引导类加载器,然而引导类加载器中java.lang.String中没有主方法main,应该使用应用程序加载器来加载。

    双亲委派机制的优势:

    避免类的重复加载。父类加载了,子类就不用加载

    保护程序安全,防止核心API被随意篡改

    例如:java.lang.String

    沙箱安全机制

    自定义string类,但是在加载自定义String类的时候会率先使用引导类加载器加载,而引导类加载器在加载的过程中会先加载jdk自带的文件(rt.jar包中java\lang\string.class),报错信息说没有main方法,就是因为加载的是rt.jar包中的String类。这样可以保证对java核心源代码的保护,这就是沙箱安全机制。

    在JVM中表示两个class对象是否为同一个类存在两个便要条件: 类的完整类名必须一致,包括包名加载这个类的ClassLoader(指ClassLoader实例对象)必须相同
    Processed: 0.009, SQL: 8