自定义ClassLoader

[list][*][b]进行实验[/b][/list]
以下代码使用一个自定义类加载器,输出类加载器的层级结构和当前实例使用的类加载器
Class cl=new DefinedClassLoader("d:/").loadClass("HelloWorldServiceImpl");
HelloWorldService helloWorldService=(HelloWorldService)cl.newInstance();
helloWorldService.helloWorld();
ClassLoader loader=cl.getClassLoader();
while(loader!=null){
System.out.print(loader.getClass().getName()+"->");
loader=loader.getParent();
}
System.out.println(loader);
System.out.println("helloWorldService实例加载器:"+helloWorldService.getClass().getClassLoader());

从输出可以看出依次:自定义类加载器》应用类加载器》扩展类加载器》根加载器(Bootstrap ClassLoader,非java实现)
com.sx.assess.DefinedClassLoader->sun.misc.Launcher$AppClassLoader->sun.misc.Launcher$ExtClassLoader->null
helloWorldService实例加载器:com.risk.assess.DefinedClassLoader@48533e64

[color=blue]最终helloWorldService实例使用的是自定义类加载器,因为加载类时仅指定了类名称,AppClassLoader在classpath中找不到,最终交给DefinedClassLoader加载。
java类加载器是委托关系,入口加载类会先委托给父加载类进行加载,父加载类加载失败再移交给子类,直到加载成功为止。[/color][quote]这篇文章总结的很到位:[url]http://blog.csdn.net/gjanyanlig/article/details/6818655[/url][/quote]

[list][*][b]自定义类加载器[/b][/list]
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class DefinedClassLoader extends ClassLoader{

private String classDir;

public DefinedClassLoader(){

}

public DefinedClassLoader(String classDir){
this.classDir = classDir;
}

@SuppressWarnings("deprecation")
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
//class文件的路径
String classPathFile = classDir + "/" + name + ".class";
try {
//将class文件进行解密
FileInputStream fis = new FileInputStream(classPathFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
encodeAndDecode(fis,bos);
byte[] classByte = bos.toByteArray();
//将字节流变成一个class
return defineClass(classByte,0,classByte.length);
} catch (Exception e) {
e.printStackTrace();
}
return super.findClass(name);
}

//测试,先将HelloWorldServiceImpl.class文件加密写到工程的class_temp目录下
public static void main(String[] args) throws Exception{
//配置运行参数
String srcPath = "D:/HelloWorldServiceImpl.class";//class原路径
String desPath = "d:/class_temp";//class输出的路径
String desFileName = srcPath.substring(srcPath.lastIndexOf("/")+1);
String desPathFile = desPath + "/" + desFileName;
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(desPathFile);
//将class进行加密
encodeAndDecode(fis,fos);
fis.close();
fos.close();
}

/**
* 加密和解密算法
* @param is
* @param os
* @throws Exception
*/
private static void encodeAndDecode(InputStream is,OutputStream os) throws Exception{
int bytes = -1;
while((bytes = is.read())!= -1){
bytes = bytes ^ 0xff;//和0xff进行异或处理
os.write(bytes);
}
}

}

[list][*][b]我的问题[/b][/list]
实验中通过自定义类加载器去加载类,在实际项目中我们通常把实例交给spring容器进行管理,由FactoryBean进行创建,如何把自定义类加载与spring进行集成。有待研究。。。

[color=red]抽空研究了下,成功集成:[/color]
http://sheungxin.iteye.com/blog/2352833
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值