溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中自定義類加載器的示例分析

發(fā)布時(shí)間:2021-07-27 15:02:23 來(lái)源:億速云 閱讀:82 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹Java中自定義類加載器的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

具體如下。

自己寫(xiě)的類加載器

Java中自定義類加載器的示例分析

需要注意的是:如果想要對(duì)這個(gè)實(shí)例進(jìn)行測(cè)試的話,首先需要在c盤(pán)建立一個(gè)c://myjava的目錄。然后將相應(yīng)的java文件放在這個(gè)目錄中。并將產(chǎn)生的.clas文件放在c://myjava/com/lg.test目錄下,否則是找不到的。這是要注意的。。

class FileClassLoader :

package com.lg.test;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * Created by 鏉庢灉 on 2016/8/6.
 */
public class FileClassLoader extends ClassLoader {
	String rootDir=null;
	public FileClassLoader(String rootDir) {
		this.rootDir = rootDir;
	}
	@Override
	  protected Class<?> findClass(String className) throws ClassNotFoundException {
		//首先檢查是否已經(jīng)被加載了。
		Class<?> c = findLoadedClass(className);
		String path = rootDir + "/" + className.replace('.', '/') + ".class";
		if (c != null) {
			return c;
		} else {
			/*雙親委托模式*/
			ClassLoader loaderParent = this.getParent();
			c = loaderParent.loadClass(className);
			if (c != null) {
				return c;
			} else {
				/*如果再不行的話,就再進(jìn)行加載。因?yàn)樽止?jié)碼的本質(zhì)就是一個(gè)字節(jié)數(shù)組*/
				InputStream is = null;
				ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				try {
					is = new FileInputStream(path);
					byte[] buffer = new byte[1024];
					int len = 0;
					while ((len = is.read(buffer)) != -1) {
						outputStream.write(buffer, 0, len);
					}
					c = defineClass(className, buffer, 0, buffer.length);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
				finally {
					if (is != null) {
						try {
							is.close();
						}
						catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
			return c;
		}
	}
}

class Demo :

package com.lg.test;
/**
 * Created by 鏉庢灉 on 2016/8/6.
 */
/*相同的類加載器對(duì)同一個(gè)類進(jìn)行加載,得到的hascode是相同的
 * 不同的類加載器對(duì)同一類進(jìn)行加載,得到的hascode是不一樣的*/
public class Demo {
	public static void main(String[] args) {
		FileClassLoader loader = new FileClassLoader("c://myjava");
		FileClassLoader loader2=new FileClassLoader("c://myjava");
		try {
			Class<?> c = loader.findClass("com.lg.test.HelloWorld");
			Class<?> c0=loader.findClass("com.lg.test.HelloWorld");
			Class<?> c1=loader2.findClass("com.lg.test.HelloWorld");
			Class<?> c2=loader.findClass("com.lg.test.Demo01");
			Class<?> c3=loader.findClass("java.lang.String");
			System.out.println(c.hashCode());
			System.out.println(c.getClassLoader());
			System.out.println(c0.hashCode());
			System.out.println(c0.getClassLoader());
			System.out.println(c1.hashCode());
			System.out.println(c1.getClassLoader());
			System.out.println(c2.hashCode());
			System.out.println(c2.getClassLoader());
			System.out.println(c3.hashCode());
			System.out.println(c3.getClassLoader());
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

最后運(yùn)行的結(jié)果為:

366712642
sun.misc.Launcher$AppClassLoader@4e0e2f2a
366712642
sun.misc.Launcher$AppClassLoader@4e0e2f2a
366712642
sun.misc.Launcher$AppClassLoader@4e0e2f2a
1829164700
sun.misc.Launcher$AppClassLoader@4e0e2f2a
2018699554
null

如果是定義網(wǎng)絡(luò)類加載器的話,那么就需要使用URL來(lái)進(jìn)行了。這是要注意的。

可以將rootDie的值變?yōu)閏om.bjsxt.cn. 然后利用Url.openStream()就可以了。

以上是“Java中自定義類加載器的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI