Skip to content

Commit

Permalink
✅ 【java.lang.ClassLoader】 自定义类加载器
Browse files Browse the repository at this point in the history
  • Loading branch information
kangjianwei committed Mar 29, 2019
1 parent 8785a99 commit adf66c7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions LearningJDK.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/src/test/kang/classloader/other" />
<excludeFolder url="file://$MODULE_DIR$/src/test/kang/packagee/exclude" />
</content>
<orderEntry type="inheritedJdk" />
Expand Down
12 changes: 12 additions & 0 deletions src/test/kang/classloader/ClassLoaderTest03.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.kang.classloader;

// 自定义类加载器
public class ClassLoaderTest03 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
CustomClassLoader customClassLoader = new CustomClassLoader();

// 将编译好的User类放在{项目源码的src目录}/test/kang/classloader/other下面,然后就可以使用自定义类加载器加载了
Class<?> loadClass = customClassLoader.loadClass("test.kang.classloader.other.User");
loadClass.newInstance();
}
}
51 changes: 51 additions & 0 deletions src/test/kang/classloader/CustomClassLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package test.kang.classloader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

// 自定义类加载器,从指定的目录加载类或其他资源
public class CustomClassLoader extends ClassLoader {

// 设置父级类加载器为app class loader,保持双亲委托模式
public CustomClassLoader() {
// 类加载器名称为custom
super("custom", ClassLoader.getSystemClassLoader());
}

@Override
protected Class<?> findClass(String className) {
// 包名+类名转换为路径
String cn = className.replace('.', '/') + ".class";

URL url = findResource(cn);

try {
InputStream inputStream = url.openStream();
byte[] bytes = new byte[10240]; // 假设一次就把类文件读完了
int len = inputStream.read(bytes);
return defineClass(className, bytes, 0, len);
} catch(IOException e) {
e.printStackTrace();
}

return null;
}

@Override
protected URL findResource(String resName) {
// 获取项目根目录
String basepath = new File("").getAbsolutePath();

URL url = null;
try {
// 这里约定从项目源码的src目录开始查找资源(不是类路径)
url = new URL("file:/" + basepath + "/src/" + resName);
} catch(MalformedURLException e) {
e.printStackTrace();
}
return url;
}
}
Binary file added src/test/kang/classloader/other/User.class
Binary file not shown.
8 changes: 8 additions & 0 deletions src/test/kang/classloader/other/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test.kang.classloader.other;

public class User {
public User() {
ClassLoader classLoader = User.class.getClassLoader();
System.out.println("我是被 " + classLoader.getName() + " 加载的");
}
}

0 comments on commit adf66c7

Please sign in to comment.