mybatis源码解析(一)加载资源文件
2016-11-25 11:13
549 查看
1、通过Resources加载资源文件
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(Resources.getResourceAsStream("SqlMapperConfig.xml"));
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;
}
classLoaderWrapper
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;
}
2、Resources
重要属性:ClassLoaderWrapper 类加载器的包装对象
通过资源名称获取对应的字节输入流、字符输入流、文件、属性对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(Resources.getResourceAsStream("SqlMapperConfig.xml"));
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;
}
classLoaderWrapper
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;
}
2、Resources
重要属性:ClassLoaderWrapper 类加载器的包装对象
通过资源名称获取对应的字节输入流、字符输入流、文件、属性对象
相关文章推荐
- Mybatis3源码分析(三):解析mapper的xml配置文件
- OpenStack Cinder服务启动过程中的资源加载和扩展源码解析之三
- (三)MyBatis源码解析之配置文件
- Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
- mybatis源码学习之执行过程分析(2)——config.xml配置文件和mapper.xml映射文件解析过程
- OpenStack Cinder服务启动过程中的资源加载和扩展源码解析之一
- Mybatis3源码分析(13)-Sql解析执行-BoundSql的加载-2
- mybatis底层源码分析之--配置文件读取和解析
- 【mybatis源码分析】原理分析之三:初始化(配置文件读取和解析)
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
- Spring源码分析--Ioc容器定位解析资源文件并注册BeanDefinition
- Mybatis3源码分析(三):解析mapper的xml配置文件
- Spring源码解析和配置文件加载
- Spring IoC源码解析(一)——配置文件加载和IoC容器初始化
- spring源码解读-加载解析配置文件
- spring boot实战(第六篇)加载application资源文件源码分析
- Spring源码解析之-资源加载(1)
- MyBatis源码分析(1)-MapConfig文件的解析
- mybatis源码学习之执行过程分析(0)——配置文件加载(io包)
- CI框架源码解析十六之加载器类文件Loader.php