forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8785a99
commit adf66c7
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() + " 加载的"); | ||
} | ||
} |