类的加载器

简介: 类的加载器

总结于尚硅谷学习视频

代码

ClassLoaderTest类

package com.day0322_1;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
/**
 * 了解类的加载器
 */
public class ClassLoaderTest {
    @Test
    public void test1(){
        //对于自定义类,使用系统类加载器进行加载
        ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
        System.out.println(classLoader);
        //调用系统类加载器的getParent():获取扩展类加载器
        ClassLoader classLoader1 = classLoader.getParent();
        System.out.println(classLoader1);
        //调用扩展类加载器的getParent():无法获取引导类加载器
        //引导类加载器主要负责加载java的核心类库,无法加载自定义类的。
        ClassLoader classLoader2 = classLoader1.getParent();
        System.out.println(classLoader2);//获取不到 null
        ClassLoader classLoader3 = String.class.getClassLoader();
        System.out.println(classLoader3);//获取不到 null
    }
    /*
     Properties:用来读取配置文件。
     */
    @Test
    public void test2()throws Exception{
        Properties pros=new Properties();
        //此时的文件默认在当前的module 下。
        //读取配置文件的方式一:
//        FileInputStream fis=new FileInputStream("jdbc.properties");
        FileInputStream fis=new FileInputStream("src\\jdbc1.properties");
        pros.load(fis);
        //读取配置文件的方式二:使用ClassLoader
        //配置文件默认识别为当前module的src下
//        ClassLoader classLoader=ClassLoaderTest.class.getClassLoader();
//        InputStream is = classLoader.getResourceAsStream("jdbc1.properties");
//        pros.load(is);
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        System.out.println("user = "+user+",password = "+password);
    }
}

图片





相关文章
|
6月前
|
Java
类加载器以及类的加载过程
这篇文章讨论了Java中的类加载器机制以及类的加载过程。
类加载器以及类的加载过程
|
7月前
|
Java
类加载的常见加载顺序
这段内容是关于Java代码执行顺序的示例。代码展示了类的静态代码块、构造器和局部构造块的执行优先级:静态代码块先于构造器和局部构造块执行,并且只在类加载时运行一次。主函数执行前,会依次看到静态代码块、局部构造块(初始化变量)和构造器的输出。列表中总结了执行顺序和特点。
|
9月前
|
存储 缓存 安全
JVM 类的加载篇
JVM 类的加载篇
65 0
什么是双亲委派模型?
什么是双亲委派模型?
|
9月前
|
存储 安全 Java
JVM类加载(类加载过程、双亲委派模型)
JVM类加载(类加载过程、双亲委派模型)
|
存储 安全 Java
类加载器与类的加载过程
类加载器与类的加载过程
|
安全 前端开发 Java
双亲委派模型与类加载器
我们都知道类都是通过类加载器被加载进虚拟机中的,那这个类加载器有哪些呢?我们平时写的代码又是通过什么类加载器被加载进虚拟机中的呢?类加载器的工作模式又是什么呢?带着疑问一起去学习下双亲委派模型与类加载器。
140 0
双亲委派模型与类加载器
|
缓存 前端开发 Java
37. 请你详细说说类加载流程,类加载机制及自定义类加载器 中
37. 请你详细说说类加载流程,类加载机制及自定义类加载器 中
143 0
37. 请你详细说说类加载流程,类加载机制及自定义类加载器 中
|
安全 前端开发 Java
双亲委派模型与自定义类加载器
双亲委派模型与自定义类加载器
双亲委派模型与自定义类加载器
|
安全 Java 编译器
Java反射(三)类加载
反射机制是java实现动态语言的关键,也就是通过反射实现类动态加载
163 0