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源码分析】原理分析之三:初始化(配置文件读取和解析)
- MyBatis-3.4.2-源码分析6:解析XML之objectWrapperFactoryElement & reflectorFactoryElement
- MyBatis-3.4.2-源码分析15:XML解析之buildStatementFromContext(context.evalNodes(‘select|insert|update|delete’
- Mybatis架构设计及源码分析-mapper.xml文件解析
- MyBatis-3.4.2-源码分析19:Plugins机制研究-初探
- mybatis从配置读取到打开连接的源码分析
- Mybatis源码分析-读取非项目中的xml文件
- MyBatis-3.4.2-源码分析17:XML解析之bindMapperForNamespace
- MyBatis-3.4.2-源码分析2:解析XML之settingsAsProperties(root.evalNode("settings"))
- MyBatis-3.4.2-源码分析12:XML解析之mapperElement(root.evalNode("mappers"))
- MyBatis-3.4.2-源码分析4:解析XML之pluginElement(root.evalNode("plugins"))
- Mybatis技术(四) 从配置读取到打开连接的源码分析
- MyBatis-3.4.2-源码分析7:解析XML之settingsElement(settings)
- MyBatis-3.4.2-源码分析11:XML解析之environmentsElement+Druid的解析准备工作:整合Druid
- MyBatis-3.4.2-源码分析1:解析XML之propertiesElement(root.evalNode("properties"))
- MyBatis-3.4.2-源码分析14:XML解析之sqlElement(context.evalNodes("/mapper/sql"))
- MyBatis-3.4.2-源码分析18:XML解析之RoleMapper userMapper = sqlSession.getMapper(RoleMapper.class)
- MyBatis-3.4.2-源码分析16:XML解析之SqlSessionFactory|SqlSession
- [置顶] Mybatis技术(四) 从配置读取到打开连接的源码分析
- MyBatis-3.4.2-源码分析9:XML解析之databaseIdProviderElement(root.evalNode("databaseIdProvider"))