在之前的一篇文章浅议tomcat与classloader中已经介绍过ClassLoader在tomcat中的应用,但是相对来说都是对双亲委派模型的比较正统的应用。
这里再简单的介绍一下双亲委派模型,每次从底层的ClassLoader申请类加载之后都会自底向上判断这个类是否被加载过,然后再自顶向下的去加载类。双亲委派模型很简单,在ClassLoader中短短的loadClass方法就能搞定他。

protected synchronized Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
// First, check if the class has already been loaded
Class c = findLoadedClass(name);
if (c == null) {
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent cl

本文通过jdbc示例深入探讨了如何绕过双亲委派模型加载类,特别是分析了DriverManager.getConnection方法中Thread.currentThread().getContextClassLoader()的角色,解释了为何在驱动已经被加载的情况下仍需要检查加载器的一致性,以确保安全性。

2万+

被折叠的 条评论
为什么被折叠?



