获取—实现类加载器的方式

获取类加载器的几种方式:线程、Class、已知类加载器
(1) ClassLoader classLoader1 = Thread.currentThread().getContextClassLoader();
(2) ClassLoader classLoader2 = ClassName.class.getClassLoader();
(3) ClassLoader classLoader3 = Class.forName(“classpath”).getClassLoader();
自定义类加载器:
1、为何要自定义类加载器?
    JVM提供的类加载器,只能加载指定目录的jar和class,如果我们想加载其他位置的类或jar时,例如加载网络上的一个class文件,默认的ClassLoader就不能满足我们的需求了,所以需要定义自己的类加载器。
2、实现自定义类加载器方式:
    方式一:继承ClassLoader,重写父类的findClass()方法
    方式二:继承URLClassLoader类,然后设置自定义路劲的URL来加载URL下的类。将指定目录转换为URL路径,然后重写findClass()方法。

猜你喜欢

转载自blog.csdn.net/pc_zkr/article/details/82427921