1.双亲委派机制:
- 优势:
- 缺点:
2.破坏双亲委派机制:
- 第一次破坏:
- 第二次破坏:线程上下文类加载器:
- 第三次破坏:
3.热替换:
4.沙箱安全机制:
- JDK1.0:
- JDK1.1:
- JDK1.2:
- JDK1.6:
5.自定义类加载器:
- 实现:
- 代码实现:
public class MyClassLoader extends ClassLoader{
private String bytePath;
public MyClassLoader(String bytePath) {
this.bytePath = bytePath;
}
public MyClassLoader(ClassLoader parent, String bytePath) {
super(parent);
this.bytePath = bytePath;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
try {
// 获取字节码文件的完整路径
String fileName = bytePath+name+".class";
// 获取一个输入流
bis = new BufferedInputStream(new FileInputStream(fileName));
// 获取一个输出流
bos = new ByteArrayOutputStream();
// 具体读入数据写出数据的过程
int len;
byte[] data = new byte[1024];
while ((len = (bis.read(data)))!=-1){
bos.write(data,0,len);
}
// 获取内存中完整的字节数组的数据
byte[] bytes = bos.toByteArray();
// 将字节数组的数据转化为Class的实例
Class clazz = defineClass(null,bytes,0,bytes.length);
return clazz;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
6.Java9新特性: