浅谈Java类的加载,链接及初始化

一 类生命周期

Loading

Linking(Verification、Preparation、Resolution)

Initializing

浅谈Java类的加载,链接及初始化_第1张图片

二 类加载器

1 图解

浅谈Java类的加载,链接及初始化_第2张图片

2 代码

package jvm;
 
public class T002_ClassLoadLevel {
    public static void main(String[] args) {
        System.out.println(String.class.getClassLoader());
        System.out.println(sun.awt.HKSCS.class.getClassLoader());
        System.out.println(sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader());
        System.out.println(T002_ClassLoadLevel.class.getClassLoader());
        System.out.println(sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader().getClass().getClassLoader());
        System.out.println(T002_ClassLoadLevel.class.getClassLoader().getClass().getClassLoader());
    }
}

3 测试 

null

null

sun.misc.Launcher$ExtClassLoader@5b2133b1

sun.misc.Launcher$AppClassLoader@18b4aac2

null

null

三 反射执行过程

浅谈Java类的加载,链接及初始化_第3张图片

四 双亲委派和父加载器

1 父加载器

父加载器不是类加载器的加载器。也不是类加载器的父类加载器。

2 双亲委派是一个孩子向父亲方向检查,然后父亲向孩子方向加载的双亲委派过程。

3 为什么要搞双亲委派

主要是出于安全原因,如果没有双亲委派,开发人员可以开发出自己的 String,用它来替换核心类库中的 String,在自定义的 String 类中收集用户信息,导致系统不安全。

4 代码

package jvm;
 
public class T004_ParentAndChild {
    public static void main(String[] args) {
        System.out.println(T004_ParentAndChild.class.getClassLoader());
        System.out.println(T004_ParentAndChild.class.getClassLoader().getClass().getClassLoader());
        System.out.println(T004_ParentAndChild.class.getClassLoader().getParent());
        System.out.println(T004_ParentAndChild.class.getClassLoader().getParent().getParent());
    }
}

5 测试

sun.misc.Launcher$AppClassLoader@18b4aac2

null

sun.misc.Launcher$ExtClassLoader@1be6f5c3

null

6 双亲委派过程

浅谈Java类的加载,链接及初始化_第4张图片

五 类加载器范围

浅谈Java类的加载,链接及初始化_第5张图片

1 代码

package jvm;
 
public class T003_ClassLoaderScope {
    public static void main(String[] args) {
        System.out.println("----------------------------------------------------------");
        String pathBoot = System.getProperty("sun.boot.class.path");
        System.out.println(pathBoot.replaceAll(";",System.lineSeparator()));
        System.out.println("----------------------------------------------------------");
        String pathExt = System.getProperty("java.ext.dirs");
        System.out.println(pathExt.replaceAll(";",System.lineSeparator()));
        System.out.println("----------------------------------------------------------");
        String pathApp = System.getProperty("java.class.path");
        System.out.println(pathApp.replaceAll(";",System.lineSeparator()));
    }
}

2 测试

----------------------------------------------------------
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\resources.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\rt.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\sunrsasign.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\jsse.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\jce.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\charsets.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\jfr.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\classes
----------------------------------------------------------
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext
C:\WINDOWS\Sun\Java\lib\ext
----------------------------------------------------------
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\charsets.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\deploy.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\access-bridge-64.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\cldrdata.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\dnsns.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\jaccess.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\jfxrt.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\localedata.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\nashorn.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\sunec.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\sunjce_provider.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\sunmscapi.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\sunpkcs11.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\ext\zipfs.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\javaws.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\jce.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\jfr.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\jfxswt.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\jsse.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\management-agent.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\plugin.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\resources.jar
D:\ProgramFiles\Java\jdk1.8.0_251\jre\lib\rt.jar
E:\javatest1\target\classes
D:\.m2\repos\org\springframework\boot\spring-boot-starter\2.2.6.RELEASE\spring-boot-starter-2.2.6.RELEASE.jar
D:\.m2\repos\org\springframework\boot\spring-boot\2.2.6.RELEASE\spring-boot-2.2.6.RELEASE.jar
D:\.m2\repos\org\springframework\spring-context\5.2.5.RELEASE\spring-context-5.2.5.RELEASE.jar
D:\.m2\repos\org\springframework\spring-aop\5.2.5.RELEASE\spring-aop-5.2.5.RELEASE.jar
D:\.m2\repos\org\springframework\spring-beans\5.2.5.RELEASE\spring-beans-5.2.5.RELEASE.jar
D:\.m2\repos\org\springframework\spring-expression\5.2.5.RELEASE\spring-expression-5.2.5.RELEASE.jar
D:\.m2\repos\org\springframework\boot\spring-boot-autoconfigure\2.2.6.RELEASE\spring-boot-autoconfigure-2.2.6.RELEASE.jar
D:\.m2\repos\org\springframework\boot\spring-boot-starter-logging\2.2.6.RELEASE\spring-boot-starter-logging-2.2.6.RELEASE.jar
D:\.m2\repos\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar
D:\.m2\repos\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar
D:\.m2\repos\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar
D:\.m2\repos\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar
D:\.m2\repos\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar
D:\.m2\repos\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar
D:\.m2\repos\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar
D:\.m2\repos\org\springframework\spring-core\5.2.5.RELEASE\spring-core-5.2.5.RELEASE.jar
D:\.m2\repos\org\springframework\spring-jcl\5.2.5.RELEASE\spring-jcl-5.2.5.RELEASE.jar
D:\.m2\repos\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar
D:\.m2\repos\commons-beanutils\commons-beanutils\1.9.4\commons-beanutils-1.9.4.jar
D:\.m2\repos\commons-logging\commons-logging\1.2\commons-logging-1.2.jar
D:\.m2\repos\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar
D:\.m2\repos\commons-codec\commons-codec\1.14\commons-codec-1.14.jar
D:\.m2\repos\com\google\code\gson\gson\2.8.6\gson-2.8.6.jar
D:\.m2\repos\org\apache\commons\commons-lang3\3.9\commons-lang3-3.9.jar
D:\.m2\repos\cglib\cglib\2.2.2\cglib-2.2.2.jar
D:\.m2\repos\asm\asm\3.3.1\asm-3.3.1.jar
D:\.m2\repos\commons-lang\commons-lang\2.6\commons-lang-2.6.jar
D:\.m2\repos\com\mchange\c3p0\0.9.5.2\c3p0-0.9.5.2.jar
D:\.m2\repos\com\mchange\mchange-commons-java\0.2.11\mchange-commons-java-0.2.11.jar
D:\.m2\repos\org\projectlombok\lombok\1.18.12\lombok-1.18.12.jar
D:\.m2\repos\com\google\guava\guava\20.0\guava-20.0.jar
D:\.m2\repos\commons-io\commons-io\2.6\commons-io-2.6.jar
D:\.m2\repos\com\alibaba\fastjson\1.2.31\fastjson-1.2.31.jar
D:\.m2\repos\org\apache\poi\poi\3.9\poi-3.9.jar
D:\.m2\repos\org\apache\poi\poi-ooxml\3.9\poi-ooxml-3.9.jar
D:\.m2\repos\org\apache\poi\poi-ooxml-schemas\3.9\poi-ooxml-schemas-3.9.jar
D:\.m2\repos\org\apache\xmlbeans\xmlbeans\2.3.0\xmlbeans-2.3.0.jar
D:\.m2\repos\stax\stax-api\1.0.1\stax-api-1.0.1.jar
D:\.m2\repos\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar
D:\.m2\repos\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar
D:\ProgramFiles\JetBrains\IDEA\lib\idea_rt.jar

到此这篇关于浅谈Java类的加载,链接及初始化的文章就介绍到这了,更多相关Java类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(浅谈Java类的加载,链接及初始化)