java*м_⼸*ʲô

ڣ2021-11-30 23:25:51

ClassLoader.getSystemClassLoader() ϵͳ*Application ClassLoaderڼClassPathµ࣬˫ίģ͡


Thread.currentThread().getContextClassLoader() ߳*ڽ⡣


getClass().getClassLoader() ǰ*


ϸ˵*


*IJνṹ


ῴɷ塱վJavaǶȿֻ2ֲͬ͵*һ*(Bootstrap ClassLoader)*C++ʵֵģһ֣һ**ⲿģJavaʵ֣ȫ̳Գjava.lang.ClassLoaderվJavaԱǶȿԷֳ3֣*ᵽؼشJAVA_HOME/lib·е߱-Xbootclasspathָ·µࡣ*޷Javaֱá


չ*(Extension ClassLoader),*sum.misc.Launcher$ExtClassLoaderʵ֣JAVA_HOME/lib/ext·µ࣬java.ext.dirsϵͳָµ⡣


Ӧó*(Application ClassLoader),*sum.misc.Launcher$AppClassLoaderʵ֡*ClassLoader.getSystemClassLoader()ȡҲϵͳ*AppClassLoaderӼû·(ClassPath)ָ⣬߿ֱʹá


*IJνṹ


˫ίģ


*˫ίģJDK1.2뱻ӦڼеJavaСһǿԵԼģͣJavaƼһطʽ


˫ίó(ܹھ)ģͿԱ֤JavaȶУֹظغ޸ġǾأ


˫ίģ͵Ĺ£һ*յصȲԼԼ࣬ǰίɸ*ȥɣͼʾÿһε*ˣеļնӦô͵*Уֻи෴Լ޷ʱ(Χûҵָ)ּ*Ż᳢Լء


java.lang.Objectĸ*Ҫ࣬նBootstrap ClassLoaderأObjectڳĸ*ͬһࡣ෴˫ίģͽмأûԶһObjectಢ·£տܻҡ


˫ίģͺܺõؽ˻ͳһ⣬֤İȫԡ


˫ίģ


߳*


˫ίģڴ󲿷ֳҲȱݣBootstrap*أǻҪصûĴ룬ȴӦ*أʱôأ


JNDI(Java Naming and Directory Interface)ֳJNDIJavaı׼ĴBootstrap*أJNDIĿľǶԴмйͲңҪöʵֵJNDIӿṩ(SPI)Ĵ룬ṩĴjarClassPath£ʹ˫ίģͼķʽǸ㲻ģôأ


Ϊ˽JavaŶ߳*(Thread Context ClassLoader),Ȼȷʵ̫ţⰡ*ͨjava.lang.ThreadsetContextClassLoader()ã̻߳δãӸ߳м̳һӦóȫַΧڶûùô*AppClassLoader߳*JNDIͿԼҪSPI룬**ضʵΥ˫ίԭġʵJNDIJDBCJCEJAXBJBI漰SPIضĻȡַʽ


ܽ᣺߳ļ*֮Դ˫ίģΪ˫ίģĵһģܽ*ȥ*淽


Tomcat*


ʵϣֻDriverʵֻҪҪ˫ίɻ޷ǰ£tomcatspringȵȵҲͨһЩֶƹ˫ίɻơ


˫ίģҪ˶*֮⣬*ӦԼĸ*ءtomcat ΪʵָԣûԼÿwebappClassLoaderԼĿ¼µclassļᴫݸ*ͼʾ


ͼеίɹϵпԿCommonClassLoaderܼص඼ԱCatalina ClassLoaderSharedClassLoaderʹãӶʵ˹Ĺá


CatalinaClassLoaderShared ClassLoaderԼܼصԷ໥롣


WebAppClassLoaderʹSharedClassLoaderص࣬WebAppClassLoaderʵ֮໥롣


JasperLoaderļطΧJSPļһ.ClassļֵĿľΪ˱Web⵽JSPļ޸ʱ滻ĿǰJasperLoaderʵͨٽһµJsp*ʵJSPļHotSwapܡ


ܽ᣺tomcat֮ƻ˫ίģͣҪ˫ίģֻ˹ԣûп󣬼Ĺ


OSGI*


˫ίģ͵һԳ̬׷󡣱滻(HotSwap)ģȲ(Hot Deployment)USBȲμȷȲϵͳ˵кܴͣɲЧʰ


OSGIJavaģ黯׼OSGIʵģȲĹؼԶ*ÿһģ鶼һԼ*ҪһBundleʱBundleͬ*һͬ滻ʵ滻


OSGI£*ͽṹ˫ίģͣ״ṹյʱOSGIǰ˳ģԡjava.*ͷ࣬ίɸ*ء


ίбڵ࣬ίɸ*ء


Importбе࣬ίɸExportBoundle*ء


ҵǰBoundleClassPathʹԼ*ء


ǷFragment BoundleУڣίɸFragment Boundle*ء


Dynamic ImportбBoundleίɸӦBoundle*ء


϶δѯʧܡ


˳1,2˫ίƣ඼*ع̡


ܽ᣺OSGI Boundle*ṩصһֻƣ*ṹһǵͽṹҲ״ṹ

Ƽ