您的位置:首页 > 其它

MyBatis-3.4.2-源码分析0:读取XML文件

2017-03-25 00:00 357 查看
String resource="mybatis-config.xml";
InputStream inputStream =Resources.getResourceAsStream(resource);

我们来看看如何读取文件的,进入此函数

/*
* Returns a resource on the classpath as a Stream object
*
* @param resource The resource to find
*
* @return The resource
*
* @throws java.io.IOException If the resource cannot be found or read
*/
public static InputStream getResourceAsStream(String resource) throws IOException {
// 看到这里了
return getResourceAsStream(null, resource);

}

进入getResourceAsStream函数,继续跟踪

/*
* Returns a resource on the classpath as a Stream object
*
* @param loader The classloader used to fetch the resource
*
* @param resource The resource to find
*
* @return The resource
*
* @throws java.io.IOException If the resource cannot be found or read
*/
public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {

InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
if (in == null) {
throw new IOException("Could not find resource " + resource);
}
return in;

}

private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();

这里的class装载器是一个静态变量,进入其getResourceAsStream函数

---

/*
* Get a resource from the classpath, starting with a specific class loader
*
* @param resource - the resource to find
*
* @param classLoader - the first class loader to try
*
* @return the stream or null
*/
public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {

return getResourceAsStream(resource, getClassLoaders(classLoader));

}

先分析getClassLoaders的执行

ClassLoader[] getClassLoaders(ClassLoader classLoader) {
//
return new ClassLoader[] {
classLoader, //null
defaultClassLoader, Thread.currentThread().getContextClassLoader(),
getClass().getClassLoader(), systemClassLoader };
}

通过debug,相关的值打印在这里

Step completed: "thread=main", org.apache.ibatis.io.ClassLoaderWrapper.getClassLoaders(), line=205 bci=0
205        return new ClassLoader[]{

main[1] step
>
Step completed: "thread=main", org.apache.ibatis.io.ClassLoaderWrapper.getClassLoaders(), line=206 bci=6
206            classLoader,

main[1] print classLoader
classLoader = null
main[1] step
>
Step completed: "thread=main", org.apache.ibatis.io.ClassLoaderWrapper.getClassLoaders(), line=207 bci=10
207            defaultClassLoader,

main[1] print defaultClassLoader
defaultClassLoader = null
main[1] step
>
Step completed: "thread=main", org.apache.ibatis.io.ClassLoaderWrapper.getClassLoaders(), line=208 bci=17
208            Thread.currentThread().getContextClassLoader(),

main[1] step
>
Step completed: "thread=main", org.apache.ibatis.io.ClassLoaderWrapper.getClassLoaders(), line=209 bci=26
209            getClass().getClassLoader(),

main[1] step
>
Step completed: "thread=main", org.apache.ibatis.io.ClassLoaderWrapper.getClassLoaders(), line=210 bci=36
210            systemClassLoader};

main[1] print systemClassLoader
systemClassLoader = "sun.misc.Launcher$AppClassLoader@18b4aac2"
main[1] step

很简单,没啥好说的。

---然后进入原先的getResourceAsStream函数

/*
* Try to get a resource from a group of classloaders
*
* @param resource - the resource to get
*
* @param classLoader - the classloaders to examine
*
* @return the resource or null
*/
InputStream getResourceAsStream(String resource, ClassLoader[] classLoader) {
// 看到这里了
for (ClassLoader cl : classLoader) {
if (null != cl) {

// try to find the resource as passed
InputStream returnValue = cl.getResourceAsStream(resource);

// now, some class loaders want this leading "/", so we'll add
// it and try again if we didn't find the resource
if (null == returnValue) {
returnValue = cl.getResourceAsStream("/" + resource);
}

if (null != returnValue) {
return returnValue;
}
}
}
return null;
}

可以看到,主要是轮询每一个类加载器,可以加载到inputstream,就返回。

很简单,加载XML完毕!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MyBatis
相关文章推荐