这里通过自定义类加载器,分别实现了对class文件加密和打破双亲委派机制的热更新(用于练习和学习)
代码结构
源代码
package com.myspringboot.jvm.loader;
import java.io.*;
/**
* 带加密功能的自定义类加载器(打破双亲委派机制,实现热加载)
*/
public class MyClassLoaderWithEncription extends ClassLoader {
private String pathName;
public MyClassLoaderWithEncription(String pathName) {
this.pathName = pathName;
}
@Override
protected Class<?> findClass(String className) throws ClassNotFoundException {
try (FileInputStream fis = new FileInputStream(new File(pathName));
ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
byte[] buffer = new byte[1024];
int size = 0;
while ((size = fis.read(buffer)) != -1) {
baos.write(getNewBuffer(buffer)