JavaSE之ClassLoader
A class loader is an object that is responsible for loading classes.
類加載器是負(fù)責(zé)加載class的一種對(duì)象.
The class ClassLoader is an abstract class.
類ClassLoader是一個(gè)抽象類.
Given the?binary name?of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class.
對(duì)于給定的一個(gè)類的完全限定名, 一個(gè)類加載器應(yīng)該試著去定位或者生成”這個(gè)類的定義數(shù)據(jù)”.
A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
一個(gè)典型的策略是將這個(gè)名字轉(zhuǎn)換成一個(gè)文件的名字, 然后從文件系統(tǒng)中讀取這個(gè)名字的class文件.
?
Every Class object contains a reference to the ClassLoader that defined it.
每一個(gè)Class對(duì)象都包含一個(gè)指向加載它的ClassLoader的引用.
?
Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime.
數(shù)組類的Class對(duì)象并不是類加載器創(chuàng)建的, 而是根據(jù)需要自動(dòng)地被JVM創(chuàng)建出來(lái)的.
The class loader for an array class, as returned by Class.getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.
數(shù)組類通過(guò)Class.getClassLoader()返回的類加載器是這個(gè)數(shù)組包含的元素類型的類加載器; 如果元素類型是原始類型的話, 那這個(gè)數(shù)組就沒(méi)有類加載器.
?
Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.
實(shí)現(xiàn)了ClassLoader的子類的應(yīng)用是為了擴(kuò)展JVM動(dòng)態(tài)加載類的方式.
?
Class loaders may typically be used by security managers to indicate security domains.
類加載器可能被典型地應(yīng)用在security manager中, 用來(lái)表明安全的區(qū)域.
?
The?ClassLoader?class uses a delegation model to search for classes and resources.
ClassLoader類使用委托模型來(lái)搜索類文件和各種資源文件.
Each instance of?ClassLoader?has an associated parent class loader.
每一個(gè)ClassLoader的實(shí)例都關(guān)聯(lián)著parent的類加載器.
When requested to find a class or resource, a?ClassLoader?instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.
當(dāng)被請(qǐng)求查找一個(gè)類文件或資源文件時(shí), 一個(gè)ClassLoader實(shí)例將會(huì)委托這個(gè)查找給它的父親類加載器, 只有父親類加載器沒(méi)有找到這個(gè)類文件或資源文件時(shí), 才會(huì)自己去查找.
The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of a?ClassLoader?instance.
JVM內(nèi)置的類加載器, 叫做”bootstrap class loader”, 它自己并沒(méi)有一個(gè)父親類加載器, 但是可能會(huì)作為其他類加載器的父親.
?
Class loaders that support concurrent loading of classes are known as?parallel capable?class loaders and are required to register themselves at their class initialization time by invoking the?ClassLoader.registerAsParallelCapable?method.
支持并行加載的類加載器被稱為"parallel capable類加載器”, 它們被要求在類的初始化的時(shí)候通過(guò)調(diào)用”ClassLoader.registerAsParallelCapable”方法來(lái)注冊(cè).
Note that the?ClassLoader?class is registered as parallel capable by default. However, its subclasses still need to register themselves if they are parallel capable.
注意:?ClassLoader類默認(rèn)已經(jīng)被注冊(cè)為”parallel capable”, 然而它的子類仍然需要注冊(cè)
In environments in which the delegation model is not strictly hierarchical, class loaders need to be parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the duration of the class loading process (see?loadClass?methods).
在一些環(huán)境中, 委托模型并不是嚴(yán)格地繼承性的, 因?yàn)轭惣虞d器需要是并行的. 否則的話, 類在加載過(guò)程中可能會(huì)導(dǎo)致死鎖, 由于loader lock在類加載的過(guò)程中被保持了. (參見(jiàn)loadClass方法.)
?
Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner.?
正常來(lái)說(shuō), JVM以一種系統(tǒng)獨(dú)立的方式從本地文件系統(tǒng)加載class文件.
For example, on UNIX systems, the virtual machine loads classes from the directory defined by the?CLASSPATH?environment variable.
例如 ,在unix系統(tǒng)上, JVM從”CLASSPATH"這個(gè)環(huán)境變量定義的路徑中加載class文件.
However, some classes may not originate from a file; they may originate from other sources, such as the network, or they could be constructed by an application.
然而, 一些class可能不是來(lái)源于文件, 它們可能出自于其他的來(lái)源, 比如網(wǎng)絡(luò)或者被一個(gè)應(yīng)用構(gòu)建出來(lái).
The method?defineClass?converts an array of bytes into an instance of class?Class. Instances of this newly defined class can be created using?Class.newInstance.
defineClass方法將一個(gè)字節(jié)數(shù)組轉(zhuǎn)換為一個(gè)Class類的實(shí)例. 這個(gè)新定義的Class類的實(shí)例可以通過(guò)方法Class.newInstance創(chuàng)建.
?
The methods and constructors of objects created by a class loader may reference other classes.
通過(guò)類加載器創(chuàng)建的方法和構(gòu)造器可能引用其他的類.
To determine the class(es) referred to, the Java virtual machine invokes the?loadClass?method of the class loader that originally created the class.
為了判斷引用的類型, JVM調(diào)用最初創(chuàng)建這個(gè)類的類加載器的loadClass方法
?
For example, an application could create a network class loader to download class files from a server. Sample code might look like:
ClassLoader loader?= new NetworkClassLoader(host,?port); Object main?= loader.loadClass("Main", true).newInstance(); .?.?.The network class loader subclass must define the methods?findClass?and?loadClassData?to load a class from the network. Once it has downloaded the bytes that make up the class, it should use the method?defineClass?to create a class instance. A sample implementation is:
class NetworkClassLoader extends ClassLoader { String host; int port; public Class findClass(String name) { byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); } private byte[] loadClassData(String name) { // load the class data from the connection ? .?.?. } } ?Binary names
Any class name provided as a?String?parameter to methods in?ClassLoader?must be a binary name as defined by?The Java? Language Specification.
Examples of valid class names include:
"java.lang.String" "javax.swing.JSpinner$DefaultEditor" "java.security.KeyStore$Builder$FileBuilder$1" "java.net.URLClassLoader$3$1"轉(zhuǎn)載于:https://www.cnblogs.com/wanquanyou/p/7261953.html
總結(jié)
以上是生活随笔為你收集整理的JavaSE之ClassLoader的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Laravel 的数据库迁移
- 下一篇: Mozilla “Common Voic