René's URL Explorer Experiment


Title: GitHub - jsonricks/classlearning: 类加载机制

Open Graph Title: GitHub - jsonricks/classlearning: 类加载机制

X Title: GitHub - jsonricks/classlearning: 类加载机制

Description: 类加载机制. Contribute to jsonricks/classlearning development by creating an account on GitHub.

Open Graph Description: 类加载机制. Contribute to jsonricks/classlearning development by creating an account on GitHub.

X Description: 类加载机制. Contribute to jsonricks/classlearning development by creating an account on GitHub.

Opengraph URL: https://github.com/jsonricks/classlearning

X: @github

direct link

Domain: patch-diff.githubusercontent.com

route-pattern/:user_id/:repository
route-controllerfiles
route-actiondisambiguate
fetch-noncev2:900c9b6d-df45-2d79-ad3a-cd2ac2cbe675
current-catalog-service-hashf3abb0cc802f3d7b95fc8762b94bdcb13bf39634c40c357301c4aa1d67a256fb
request-idB394:3966A1:8581CE:B554AA:6977E402
html-safe-nonce8341512e45ffafe90f3c99829740f3cbf406f7ff881d27a7d3b7f77174f94875
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzk0OjM5NjZBMTo4NTgxQ0U6QjU1NEFBOjY5NzdFNDAyIiwidmlzaXRvcl9pZCI6IjY4NTY2MTYzMzg2MzQ4MjY3NTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacfca704d87904ab72e527eb22e635431c510b74b6c918383b8dba64ad90e943d4
hovercard-subject-tagrepository:417332591
github-keyboard-shortcutsrepository,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location//
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/jsonricks/classlearning
twitter:imagehttps://opengraph.githubassets.com/845fa06717cd505bb024a02a6951787c9119f495ac46fd1ca6750a96dd785171/jsonricks/classlearning
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/845fa06717cd505bb024a02a6951787c9119f495ac46fd1ca6750a96dd785171/jsonricks/classlearning
og:image:alt类加载机制. Contribute to jsonricks/classlearning development by creating an account on GitHub.
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None870e7b4ef171b9ec3e0c7f1f5cb9c5a5c1d1899865e861b49008c3f435fc1c4e
turbo-cache-controlno-preview
go-importgithub.com/jsonricks/classlearning git https://github.com/jsonricks/classlearning.git
octolytics-dimension-user_id71300284
octolytics-dimension-user_loginjsonricks
octolytics-dimension-repository_id417332591
octolytics-dimension-repository_nwojsonricks/classlearning
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forktrue
octolytics-dimension-repository_parent_id129193100
octolytics-dimension-repository_parent_nwowanwujiedao/classlearning
octolytics-dimension-repository_network_root_id129193100
octolytics-dimension-repository_network_root_nwowanwujiedao/classlearning
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasec3b778ddc3525ff438f1b4bb848d8fd3aaf2e0dd
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/jsonricks/classlearning#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fjsonricks%2Fclasslearning
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fjsonricks%2Fclasslearning
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E&source=header-repo&source_repo=jsonricks%2Fclasslearning
Reloadhttps://patch-diff.githubusercontent.com/jsonricks/classlearning
Reloadhttps://patch-diff.githubusercontent.com/jsonricks/classlearning
Reloadhttps://patch-diff.githubusercontent.com/jsonricks/classlearning
jsonricks https://patch-diff.githubusercontent.com/jsonricks
classlearninghttps://patch-diff.githubusercontent.com/jsonricks/classlearning
wanwujiedao/classlearninghttps://patch-diff.githubusercontent.com/wanwujiedao/classlearning
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fjsonricks%2Fclasslearning
Fork 0 https://patch-diff.githubusercontent.com/login?return_to=%2Fjsonricks%2Fclasslearning
Star 0 https://patch-diff.githubusercontent.com/login?return_to=%2Fjsonricks%2Fclasslearning
0 stars https://patch-diff.githubusercontent.com/jsonricks/classlearning/stargazers
1 fork https://patch-diff.githubusercontent.com/jsonricks/classlearning/forks
Branches https://patch-diff.githubusercontent.com/jsonricks/classlearning/branches
Tags https://patch-diff.githubusercontent.com/jsonricks/classlearning/tags
Activity https://patch-diff.githubusercontent.com/jsonricks/classlearning/activity
Star https://patch-diff.githubusercontent.com/login?return_to=%2Fjsonricks%2Fclasslearning
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fjsonricks%2Fclasslearning
Code https://patch-diff.githubusercontent.com/jsonricks/classlearning
Pull requests 0 https://patch-diff.githubusercontent.com/jsonricks/classlearning/pulls
Actions https://patch-diff.githubusercontent.com/jsonricks/classlearning/actions
Projects 0 https://patch-diff.githubusercontent.com/jsonricks/classlearning/projects
Security 0 https://patch-diff.githubusercontent.com/jsonricks/classlearning/security
Insights https://patch-diff.githubusercontent.com/jsonricks/classlearning/pulse
Code https://patch-diff.githubusercontent.com/jsonricks/classlearning
Pull requests https://patch-diff.githubusercontent.com/jsonricks/classlearning/pulls
Actions https://patch-diff.githubusercontent.com/jsonricks/classlearning/actions
Projects https://patch-diff.githubusercontent.com/jsonricks/classlearning/projects
Security https://patch-diff.githubusercontent.com/jsonricks/classlearning/security
Insights https://patch-diff.githubusercontent.com/jsonricks/classlearning/pulse
Brancheshttps://patch-diff.githubusercontent.com/jsonricks/classlearning/branches
Tagshttps://patch-diff.githubusercontent.com/jsonricks/classlearning/tags
https://patch-diff.githubusercontent.com/jsonricks/classlearning/branches
https://patch-diff.githubusercontent.com/jsonricks/classlearning/tags
5 Commitshttps://patch-diff.githubusercontent.com/jsonricks/classlearning/commits/master/
https://patch-diff.githubusercontent.com/jsonricks/classlearning/commits/master/
imghttps://patch-diff.githubusercontent.com/jsonricks/classlearning/tree/master/img
imghttps://patch-diff.githubusercontent.com/jsonricks/classlearning/tree/master/img
src/main/java/com/dao/classloadinghttps://patch-diff.githubusercontent.com/jsonricks/classlearning/tree/master/src/main/java/com/dao/classloading
src/main/java/com/dao/classloadinghttps://patch-diff.githubusercontent.com/jsonricks/classlearning/tree/master/src/main/java/com/dao/classloading
JVM 调优.mdhttps://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/JVM%20%E8%B0%83%E4%BC%98.md
JVM 调优.mdhttps://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/JVM%20%E8%B0%83%E4%BC%98.md
ReadMe.mdhttps://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/ReadMe.md
ReadMe.mdhttps://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/ReadMe.md
pom.xmlhttps://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/pom.xml
pom.xmlhttps://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/pom.xml
READMEhttps://patch-diff.githubusercontent.com/jsonricks/classlearning
https://patch-diff.githubusercontent.com/jsonricks/classlearning#类加载机制的奥妙
https://patch-diff.githubusercontent.com/jsonricks/classlearning#1什么是类的加载
https://patch-diff.githubusercontent.com/jsonricks/classlearning#类的加载指的是将类的class文件中的二进制数据读入到内存中将其放在运行时数据区的方法区内然后在堆区创建一个javalangclass对象用来封装类在方法区内的数据结构类的加载的最终产品是位于堆区中的class对象class对象封装了类在方法区内的数据结构并且向java程序员提供了访问方法区内的数据结构的接口
https://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/img/JVM.png
https://patch-diff.githubusercontent.com/jsonricks/classlearning#类加载器并不需要等到某个类被首次主动使用时再加载它jvm-规范允许类加载器在预料某个类将要被使用时就预先加载它如果在预先加载的过程中遇到了class-文件缺失或存在错误类加载器必须在程序首次主动使用该类时才报告错误linkageerror-错误如果这个类一直没有被程序主动使用那么类加载器就不会报告错误
https://patch-diff.githubusercontent.com/jsonricks/classlearning#2类的生命周期
https://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/img/class_loading.png
https://patch-diff.githubusercontent.com/jsonricks/classlearning#其中类加载的过程包括了加载验证准备解析初始化五个阶段在这五个阶段中加载验证准备和初始化这四个阶段发生的顺序是确定的而解析阶段则不一定它在某些情况下可以在初始化阶段之后开始这是为了支持-java-语言的运行时绑定也成为动态绑定或晚期绑定另外注意这里的几个阶段是按顺序开始而不是按顺序进行或完成因为这些阶段通常都是互相交叉地混合进行的通常在一个阶段执行的过程中调用或激活另一个阶段
https://patch-diff.githubusercontent.com/jsonricks/classlearning#查找并加载类的二进制数据加载时类加载过程的第一个阶段在加载阶段虚拟机需要完成以下三件事情
https://patch-diff.githubusercontent.com/jsonricks/classlearning#相对于类加载的其他阶段而言加载阶段准确地说是加载阶段获取类的二进制字节流的动作是可控性最强的阶段因为开发人员既可以使用系统提供的类加载器来完成加载也可以自定义自己的类加载器来完成加载
https://patch-diff.githubusercontent.com/jsonricks/classlearning#加载阶段完成后虚拟机外部的二进制字节流就按照虚拟机所需的格式存储在方法区之中而且在-java-堆中也创建一个-javalangclass-类的对象这样便可以通过该对象访问方法区中的这些数据
https://patch-diff.githubusercontent.com/jsonricks/classlearning#连接
https://patch-diff.githubusercontent.com/jsonricks/classlearning#验证是连接阶段的第一步这一阶段的目的是为了确保-class-文件的字节流中包含的信息符合当前虚拟机的要求并且不会危害虚拟机自身的安全验证阶段大致会完成4个阶段的检验动作
https://patch-diff.githubusercontent.com/jsonricks/classlearning#验证阶段是非常重要的但不是必须的它对程序运行期没有影响如果所引用的类经过反复验证那么可以考虑采用--xverifynone-参数来关闭大部分的类验证措施以缩短虚拟机类加载的时间
https://patch-diff.githubusercontent.com/jsonricks/classlearning#准备阶段是正式为类变量分配内存并设置类变量初始值的阶段这些内存都将在方法区中分配对于该阶段有以下几点需要注意
https://patch-diff.githubusercontent.com/jsonricks/classlearning#假设一个类变量的定义为public-static-int-value--3
https://patch-diff.githubusercontent.com/jsonricks/classlearning#那么变量-value-在准备阶段过后的初始值为0而不是3因为这时候尚未开始执行任何java方法而把value赋值为3的public-static指令是在程序编译后存放于类构造器方法之中的所以把value赋值为3的动作将在初始化阶段才会执行
https://patch-diff.githubusercontent.com/jsonricks/classlearning#假设上面的类变量value被定义为-public-static-final-int-value--3
https://patch-diff.githubusercontent.com/jsonricks/classlearning#编译时-javac-将会为-value-生成-constantvalue-属性在准备阶段虚拟机就会根据constantvalue-的设置将-value-赋值为-3我们可以理解为-static-final-常量在编译期就将其结果放入了调用它的类的常量池中
https://patch-diff.githubusercontent.com/jsonricks/classlearning#解析阶段是虚拟机将常量池内的符号引用替换为直接引用的过程解析动作主要针对类或接口字段类方法接口方法方法类型方法句柄和调用点限定符7类符号引用进行符号引用就是一组符号来描述目标可以是任何字面量
https://patch-diff.githubusercontent.com/jsonricks/classlearning#直接引用就是直接指向目标的指针相对偏移量或一个间接定位到目标的句柄
https://patch-diff.githubusercontent.com/jsonricks/classlearning#jvm初始化步骤
https://patch-diff.githubusercontent.com/jsonricks/classlearning#类初始化时机只有当对类的主动使用的时候才会导致类的初始化类的主动使用包括以下六种
https://patch-diff.githubusercontent.com/jsonricks/classlearning#结束生命周期
https://patch-diff.githubusercontent.com/jsonricks/classlearning#在如下几种情况下java虚拟机将结束生命周期
https://patch-diff.githubusercontent.com/jsonricks/classlearning#3类加载器
https://patch-diff.githubusercontent.com/jsonricks/classlearning#从上面的结果可以看出并没有获取到-extclassloader-的父-loader原因是-bootstrap-loader引导类加载器是用-c-语言实现的找不到一个确定的返回父-loader-的方式于是就返回-null
https://patch-diff.githubusercontent.com/jsonricks/classlearning#这几种类加载器的层次关系如下图所示
https://patch-diff.githubusercontent.com/jsonricks/classlearning/blob/master/img/calssloader.png
https://patch-diff.githubusercontent.com/jsonricks/classlearning#站在-java-虚拟机的角度来讲只存在两种不同的类加载器启动类加载器它使用-c-实现这里仅限于-hotspot也就是-jdk15-之后默认的虚拟机有很多其他的虚拟机是用java-语言实现的是虚拟机自身的一部分所有其它的类加载器这些类加载器都由-java-语言实现独立于虚拟机之外并且全部继承自抽象类-javalangclassloader这些类加载器需要由启动类加载器加载到内存中之后才能去加载其他的类
https://patch-diff.githubusercontent.com/jsonricks/classlearning#站在-java-开发人员的角度来看类加载器可以大致划分为以下三类
https://patch-diff.githubusercontent.com/jsonricks/classlearning#bootstrap-classloader负责加载存放在-jdkjrelibjdk-代表-jdk-的安装目录下同下或被--xbootclasspath-参数指定的路径中的并且能被虚拟机识别的类库如-rtjar所有的-java开头的类均被-bootstrap-classloader-加载启动类加载器是无法被-java-程序直接引用的
https://patch-diff.githubusercontent.com/jsonricks/classlearning#extension-classloader该加载器由-sunmisclauncherextclassloader-实现它负责加载-jdkjrelibext-目录中或者由-javaextdirs-系统变量指定的路径中的所有类库如-javax-开头的类开发者可以直接使用扩展类加载器
https://patch-diff.githubusercontent.com/jsonricks/classlearning#application-classloader该类加载器由-sunmisclauncherappclassloader-来实现它负责加载用户类路径classpath所指定的类开发者可以直接使用该类加载器如果应用程序中没有自定义过自己的类加载器一般情况下这个就是程序中默认的类加载器
https://patch-diff.githubusercontent.com/jsonricks/classlearning#应用程序都是由这三种类加载器互相配合进行加载的如果有必要我们还可以加入自定义的类加载器因为-jvm-自带的-classloader-只是懂得从本地文件系统加载标准的-java-class-文件因此如果编写了自己的-classloader便可以做到如下几点
https://patch-diff.githubusercontent.com/jsonricks/classlearning#4类的加载
https://patch-diff.githubusercontent.com/jsonricks/classlearning#类加载有三种方式
https://patch-diff.githubusercontent.com/jsonricks/classlearning#classforname和classloaderloadclass区别
https://patch-diff.githubusercontent.com/jsonricks/classlearning#5双亲委派模型
https://patch-diff.githubusercontent.com/jsonricks/classlearning#双亲委派模型的工作流程是如果一个类加载器收到了类加载的请求它首先不会自己去尝试加载这个类而是把请求委托给父加载器去完成依次向上因此所有的类加载请求最终都应该被传递到顶层的启动类加载器中只有当父加载器在它的搜索范围中没有找到所需的类时即无法完成该加载子加载器才会尝试自己去加载该类
https://patch-diff.githubusercontent.com/jsonricks/classlearning#6自定义类加载器
https://patch-diff.githubusercontent.com/jsonricks/classlearning#通常情况下我们都是直接使用系统类加载器但是有的时候我们也需要自定义类加载器比如应用是通过网络来传输-java-类的字节码为保证安全性这些字节码经过了加密处理这时系统类加载器就无法对其进行加载这样则需要自定义类加载器来实现自定义类加载器一般都是继承自-classloader-类从上面对loadclass方法来分析来看我们只需要重写-findclass-方法即可下面我们通过一个示例来演示自定义类加载器的流程
https://patch-diff.githubusercontent.com/jsonricks/classlearning#自定义类加载器的核心在于对字节码文件的获取如果是加密的字节码则需要在该类中对文件进行解密由于这里只是演示我并未对-class-文件进行加密因此没有解密的过程这里有几点需要注意
纯洁的微笑http://www.ityouknow.com/jvm/2017/08/19/class-loading-principle.html
JVM 调优https://github.com/wanwujiedao/classlearning/blob/master/JVM%20%E8%B0%83%E4%BC%98.md
https://patch-diff.githubusercontent.com/jsonricks/classlearning#jvm-调优
Readme https://patch-diff.githubusercontent.com/jsonricks/classlearning#readme-ov-file
Please reload this pagehttps://patch-diff.githubusercontent.com/jsonricks/classlearning
Activityhttps://patch-diff.githubusercontent.com/jsonricks/classlearning/activity
0 starshttps://patch-diff.githubusercontent.com/jsonricks/classlearning/stargazers
0 watchinghttps://patch-diff.githubusercontent.com/jsonricks/classlearning/watchers
0 forkshttps://patch-diff.githubusercontent.com/jsonricks/classlearning/forks
Report repository https://patch-diff.githubusercontent.com/contact/report-content?content_url=https%3A%2F%2Fgithub.com%2Fjsonricks%2Fclasslearning&report=jsonricks+%28user%29
Releaseshttps://patch-diff.githubusercontent.com/jsonricks/classlearning/releases
Packages 0https://patch-diff.githubusercontent.com/users/jsonricks/packages?repo_name=classlearning
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.