Java語(yǔ)言中的自定義類加載器實(shí)例解析
本文研究的主要是Java語(yǔ)言中的自定義類加載器實(shí)例解析的相關(guān)內(nèi)容,具體如下。
自己寫(xiě)的類加載器

需要注意的是:如果想要對(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()就可以了。
總結(jié)
以上就是本文關(guān)于Java語(yǔ)言中的自定義類加載器實(shí)例解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
SpringBoot利用dynamic-datasource-spring-boot-starter解決多數(shù)據(jù)源問(wèn)題
dynamic-datasource-spring-boot-starter 是一個(gè)用于在 Spring Boot 項(xiàng)目中實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的工具,下面我們看看如何使用dynamic-datasource-spring-boot-starter解決多數(shù)據(jù)源問(wèn)題吧2025-03-03
使用spring aop統(tǒng)一處理異常和打印日志方式
這篇文章主要介紹了使用spring aop統(tǒng)一處理異常和打印日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring Data JPA進(jìn)行數(shù)據(jù)分頁(yè)與排序的方法
這篇文章主要介紹了Spring Data JPA進(jìn)行數(shù)據(jù)分頁(yè)與排序的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Java ArrayList擴(kuò)容機(jī)制原理深入分析
在Java中,ArrayList是最常用的集合之一。它是一種容器,它的內(nèi)部定義了一個(gè)Object類型的數(shù)組elementData,因此可用于存儲(chǔ)任意類型的數(shù)據(jù)。我們知道,數(shù)組是長(zhǎng)度恒定的。而ArrayList相當(dāng)于是一個(gè)長(zhǎng)度可變的動(dòng)態(tài)數(shù)組,一起來(lái)看看的它的擴(kuò)容機(jī)制2023-02-02
Spring Boot使用線程池創(chuàng)建多線程的完整示例
在 Spring Boot 2 中,可以使用 @Autowired 注入 線程池(ThreadPoolTaskExecutor 或 ExecutorService),從而管理線程的創(chuàng)建和執(zhí)行,以下是使用 @Autowired 方式注入線程池的完整示例,感興趣的朋友一起看看吧2025-03-03
SpringBoot整合RabbitMQ的5種模式實(shí)戰(zhàn)
本文主要介紹了SpringBoot整合RabbitMQ的5種模式實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

