java类加载器

作用:从包含字节代码的字节流中定义出虚拟机中的class类的对象。

三种类加载器:

1)Bootstrap ClassLoader(启动类加载器)

负责加载$JAVA_HOME中jre/lib/rt.jar里所有的class,由C++实现

2)Extension ClassLoader(拓展类加载器)

负责加载java平台中扩展功能的一些jar包,包括$JAVA_HOME中jre/lib/*.jar或-Djava.ext.dirs指定目录下的jar包

3)App ClassLoader(应用程序类加载器)

负责记载classpath中指定的jar包及目录中class

下面示例使用类加载器加载属性文件

 1 /**
 2      * 简单实现读取配置文件属性
 3      * @return
 4      * @throws IOException
 5      */
 6     public Properties loadCofig() throws IOException{
 7         ClassLoader loader = this.getClass().getClassLoader();
 8         InputStream input = loader.getResourceAsStream("com/demo/blog/config.properties");
 9         if(input==null){
10             throw new IOException("找不到配置文件");
11         }
12         Properties props = new Properties();
13         props.load(input);
14         return props;
15     }

 


 

 

posted @ 2017-05-22 22:35  夏虫语冰、  阅读(96)  评论(0编辑  收藏  举报