浅谈Java类的加载,链接及初始化
一 类生命周期
Loading
Linking(Verification、Preparation、Resolution)
Initializing
二 类加载器
1 图解
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
三 反射执行过程
四 双亲委派和父加载器
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 双亲委派过程
五 类加载器范围
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:ProgramFilesJavajdk1.8.0_251jrelibresources.jar
D:ProgramFilesJavajdk1.8.0_251jrelibrt.jar
D:ProgramFilesJavajdk1.8.0_251jrelibsunrsasign.jar
D:ProgramFilesJavajdk1.8.0_251jrelibjsse.jar
D:ProgramFilesJavajdk1.8.0_251jrelibjce.jar
D:ProgramFilesJavajdk1.8.0_251jrelibcharsets.jar
D:ProgramFilesJavajdk1.8.0_251jrelibjfr.jar
D:ProgramFilesJavajdk1.8.0_251jreclasses
----------------------------------------------------------
D:ProgramFilesJavajdk1.8.0_251jrelibext
C:WINDOWSSunJavalibext
----------------------------------------------------------
D:ProgramFilesJavajdk1.8.0_251jrelibcharsets.jar
D:ProgramFilesJavajdk1.8.0_251jrelibdeploy.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextaccess-bridge-64.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextcldrdata.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextdnsns.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextjaccess.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextjfxrt.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextlocaledata.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextnashorn.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextsunec.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextsunjce_provider.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextsunmscapi.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextsunpkcs11.jar
D:ProgramFilesJavajdk1.8.0_251jrelibextzipfs.jar
D:ProgramFilesJavajdk1.8.0_251jrelibjavaws.jar
D:ProgramFilesJavajdk1.8.0_251jrelibjce.jar
D:ProgramFilesJavajdk1.8.0_251jrelibjfr.jar
D:ProgramFilesJavajdk1.8.0_251jrelibjfxswt.jar
D:ProgramFilesJavajdk1.8.0_251jrelibjsse.jar
D:ProgramFilesJavajdk1.8.0_251jrelibmanagement-agent.jar
D:ProgramFilesJavajdk1.8.0_251jrelibplugin.jar
D:ProgramFilesJavajdk1.8.0_251jrelibresources.jar
D:ProgramFilesJavajdk1.8.0_251jrelibrt.jar
E:javatest1targetclasses
D:.m2reposorgspringframeworkbootspring-boot-starter2.2.6.RELEASEspring-boot-starter-2.2.6.RELEASE.jar
D:.m2reposorgspringframeworkbootspring-boot2.2.6.RELEASEspring-boot-2.2.6.RELEASE.jar
D:.m2reposorgspringframeworkspring-context5.2.5.RELEASEspring-context-5.2.5.RELEASE.jar
D:.m2reposorgspringframeworkspring-aop5.2.5.RELEASEspring-aop-5.2.5.RELEASE.jar
D:.m2reposorgspringframeworkspring-beans5.2.5.RELEASEspring-beans-5.2.5.RELEASE.jar
D:.m2reposorgspringframeworkspring-expression5.2.5.RELEASEspring-expression-5.2.5.RELEASE.jar
D:.m2reposorgspringframeworkbootspring-boot-autoconfigure2.2.6.RELEASEspring-boot-autoconfigure-2.2.6.RELEASE.jar
D:.m2reposorgspringframeworkbootspring-boot-starter-logging2.2.6.RELEASEspring-boot-starter-logging-2.2.6.RELEASE.jar
D:.m2reposchqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar
D:.m2reposchqoslogbacklogback-core1.2.3logback-core-1.2.3.jar
D:.m2reposorgslf4jslf4j-api1.7.30slf4j-api-1.7.30.jar
D:.m2reposorgapachelogginglog4jlog4j-to-slf4j2.12.1log4j-to-slf4j-2.12.1.jar
D:.m2reposorgapachelogginglog4jlog4j-api2.12.1log4j-api-2.12.1.jar
D:.m2reposorgslf4jjul-to-slf4j1.7.30jul-to-slf4j-1.7.30.jar
D:.m2reposjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar
D:.m2reposorgspringframeworkspring-core5.2.5.RELEASEspring-core-5.2.5.RELEASE.jar
D:.m2reposorgspringframeworkspring-jcl5.2.5.RELEASEspring-jcl-5.2.5.RELEASE.jar
D:.m2reposorgyamlsnakeyaml1.25snakeyaml-1.25.jar
D:.m2reposcommons-beanutilscommons-beanutils1.9.4commons-beanutils-1.9.4.jar
D:.m2reposcommons-loggingcommons-logging1.2commons-logging-1.2.jar
D:.m2reposcommons-collectionscommons-collections3.2.2commons-collections-3.2.2.jar
D:.m2reposcommons-codeccommons-codec1.14commons-codec-1.14.jar
D:.m2reposcom\codegsongson2.8.6gson-2.8.6.jar
D:.m2reposorgapachecommonscommons-lang33.9commons-lang3-3.9.jar
D:.m2reposcglibcglib2.2.2cglib-2.2.2.jar
D:.m2reposasmasm3.3.1asm-3.3.1.jar
D:.m2reposcommons-langcommons-lang2.6commons-lang-2.6.jar
D:.m2reposcommchangec3p0 .9.5.2c3p0-0.9.5.2.jar
D:.m2reposcommchangemchange-commons-java .2.11mchange-commons-java-0.2.11.jar
D:.m2reposorgprojectlomboklombok1.18.12lombok-1.18.12.jar
D:.m2reposcom\guavaguava20.0guava-20.0.jar
D:.m2reposcommons-iocommons-io2.6commons-io-2.6.jar
D:.m2reposcomalibabafastjson1.2.31fastjson-1.2.31.jar
D:.m2reposorgapachepoipoi3.9poi-3.9.jar
D:.m2reposorgapachepoipoi-ooxml3.9poi-ooxml-3.9.jar
D:.m2reposorgapachepoipoi-ooxml-schemas3.9poi-ooxml-schemas-3.9.jar
D:.m2reposorgapachexmlbeansxmlbeans2.3.0xmlbeans-2.3.0.jar
D:.m2reposstaxstax-api1.0.1stax-api-1.0.1.jar
D:.m2reposdom4jdom4j1.6.1dom4j-1.6.1.jar
D:.m2reposxml-apisxml-apis1.0.b2xml-apis-1.0.b2.jar
D:ProgramFilesJetBrainsIDEAlibidea_rt.jar
到此这篇关于浅谈Java类的加载,链接及初始化的文章就介绍到这了,更多相关Java类内容请搜索无名以前的文章或继续浏览下面的相关文章希望大家以后多多支持无名!