</pre><p> </p><p><strong>类加载器</strong></p><p><span style="font-family:Times New Roman; font-size:14px"> Java</span>虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:<span style="font-family:Times New Roman">BootStrap</span>,<span style="font-family:Times New Roman">ExtClassLoader</span>,<a target=_blank target="_blank" name="OLE_LINK1"><span style="font-family:Times New Roman; font-size:14px">AppClassLoader</span></a><span style="font-size:14px">。类加载器也是<span style="font-family:Times New Roman">Java</span>类,因为其他是<span style="font-family:Times New Roman">Java</span>类的加载器本身也要被类加载器加载,显然必须有一个类加载器不是<span style="font-family:Times New Roman">Java</span>类,这正是<span style="font-family:Times New Roman">BootStrap</span>。<span style="font-family:Times New Roman">Java</span>虚拟机中的所有类加载器采用具有父子关系的树形结构组织,在实例化,每一个类加载器对象时,需要为其指定一个父级类加载器对象或者默认采用系统类加载器为其父级类加载。</span></p><pre code_snippet_id="507635" snippet_file_name="blog_20141104_2_893673" class="java" name="code"> public class ClassLoaderTest
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println
(
ClassLoaderTest.class.getClassLoader().getClass().getName()
);
System.out.println
(
System.class.getClassLoader()
);
ClassLoader loader = ClassLoaderTest.class.getClassLoader();
while(loader!=null)
{
System.out.println(loader.getClass().getName());
loader = loader.getParent();
}
System.out.println(loader);
}
}
类加载器的委托机制
当Java虚拟机要加载一个类时,到底派出哪个类加载器去加载呢?
首先当前线程的类加载器去加载线程过程中的第一个类。
如果类A中引用了类B,Java虚拟机将使用加载类A的类加载器来加载类B。
还可以直接调用ClassLader.loadClass()方法来指定某个类加载器去加载某个类。
每个类加载器加载类时,又委托给骑上级类加载器。
当所有祖宗类加载器没有加载到类时,回到发起者类加载器,还加载不了,则抛ClassNoFoundException,不是再去找发起者类加载器的儿子。
编写自己的类加载器
编程步骤:
1、 编写一个对文件内容进行简单加密的程序。
2、 编写了一个自己的类加载器,可实现对加密过的类进行加载和解密。
3、 编写一个程序调用类加载器加载类,在源程序中不能用该类名定义引用变量,因为编译器无法识别这个类,程序中可以除了使用ClassLoader。Load方法之外,还可以使用设置线程的上下文类加载器或者系统类加载器,然后使用Class.forName。
首先定义一个类:
import java.sql.Date;
public class ClassLoaderAttachment extends Date
{
public ClassLoaderAttachment(long l)
{
super( l);
}
public String toString()
{
return "hello,itcast";
}
}
然后下面的程序是自定义的类加载器包括对.class文件的加密和解密
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MyClassLoader extends ClassLoader
{
public static void main(String[] args) throws Exception
{
String srcPath =args[0];
String destDir = args[1];
FileInputStream fis = new FileInputStream(srcPath);
String destFileName = srcPath.substring(srcPath.lastIndexOf('\\')+1);
String destPath = destDir + "\\" + destFileName;
FileOutputStream fos = new FileOutputStream(destPath);
cypher(fis,fos);
fis.close();
fos.close();
System.out.println(new ClassLoaderAttachment(100).toString());
}
private static void cypher(InputStream ips ,OutputStream ops)
throws IOException
{
System.out.println("dfdfd");
int b = -1;
while((b=ips.read())!=-1)
{
ops.write(b^0xff);
System.out.println("xxx");
}
}
private String classDir;
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException
{
// TODO Auto-generated method stub
String classFileName = classDir + "\\" + name + ".class";
try
{
FileInputStream fis = new FileInputStream(classFileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
cypher(fis,bos);
fis.close();
byte[] bytes = bos.toByteArray();
defineClass(bytes, 0, bytes.length);
}
catch(Exception e)
{
}
return super.findClass(name);
}
public MyClassLoader()
{
}
publci MyClassLoader(String classDir)
{
this.classDir = classDir;
}
}
main函数:
import java.util.Date;
public class ClassLoaderTest
{
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
System.out.println
(
ClassLoaderTest.class.getClassLoader().getClass().getName()
);
System.out.println
(
System.class.getClassLoader()
);
ClassLoader loader = ClassLoaderTest.class.getClassLoader();
while(loader!=null)
{
System.out.println(loader.getClass().getName());
loader = loader.getParent();
}
System.out.println(loader);
Class clazz = new MyClassLoader("itcastlib").loadClass("ClassLoaderAttachment");
Date d1 =(Date) clazz.newInstance();
System.out.println(d1);
}
}