说明一
public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException {
try {
return Thread.currentThread().getContextClassLoader().loadClass("TestClass");//1. 通过当前线程获取
} catch (ClassNotFoundException e) {
try {
return Class.forName("TestClass");//2. java自身的class
} catch (ClassNotFoundException ex) {
try {
return TestUtil.class.getClassLoader().loadClass("TestClass");//4. 工具类的类加载器。
} catch (ClassNotFoundException exc) {
try {
return TestClass.class.getClassLoader().loadClass("TestClass");//3. 已知执行类的类加载器
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
}
}
}
上面这段代码是struts中的过滤器加载指定类的工具类中的一个方法,从这个方法中可以看到获取类加载器的几种方式。
通过当前线程获取
java自身的class
已知执行类的类加载器
工具类的类加载器。
总结起来是三种: 线程、Class、已知类加载器
说明二
进一步说明那三种方式
其一java 1.2以及之后:
Method method = Thread.class.getMethod(“getContextClassLoader”, null);
ClassLoader cl = (ClassLoader) method.invoke(Thread.currentThread(), null);
其二:ClassLoader cl = className.class.getClassLoader();
其三:Thread.currentThread().getContextClassLoader()
第三种是最安全的用法
打个简单的比方,你一个WEB程序,发布到Tomcat里面运行。
首先是执行Tomcat org.apache.catalina.startup.Bootstrap类,这时候的类加载器是ClassLoader.getSystemClassLoader()。
而我们后面的WEB程序,里面的jar、resources都是由Tomcat内部来加载的,所以你在代码中动态加载jar、资源文件的时候,首先应该是使用Thread.currentThread().getContextClassLoader()。如果你使用Test.class.getClassLoader(),可能会导致和当前线程所运行的类加载器不一致(因为Java天生的多线程)。
Test.class.getClassLoader()一般用在getResource,因为你想要获取某个资源文件的时候,这个资源文件的位置是相对固定的。
java的类加载机制(jvm规范)是委托模型,简单的说,如果一个类加载器想要加载一个类,首先它会委托给它的parent去加载,如果它的所有parent都没有成功的加载那么它才会自己亲自来,有点儿像儿子使唤老子的感觉。
如果你使用Test.class.getClassLoader(),可能会导致和当前线程所运行的类加载器不一致 :Class.getClassLoader() returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.