类的加载与ClassLoadr

类的加载与ClassLoader的理解

类加载器的作用:

  • 类加载器的作用:将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后在堆中生成一个代表这个类的java.lang.Class对象,作为方法区中类数据的访问。

  • 类缓存:标准的javase类加载器可以按照要求查找类,但一旦某个类被加载到类加载器中,它将维持加载(缓存)一段时间。不过jvm垃圾回收机制可以回收这些Class对象。

    三个步骤来对该类进行初始化:

              类的加载(Load)(将类的class文件读入内存并为之创建一个java.lang.Class对象。此过程由类加载器完成)

              ----->类的链接(Link)(将类的二进制数据局合并到JRE中)----->类的初始化(Initialize)(jvm复制对类进行初始化)

package Reflection1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

public class ClassLoaderTest
{
   public static void main(String[] args)
  {
       //对于自定义类,使用系统类加载器进行加载
       ClassLoader classLoaderTest=ClassLoaderTest.class.getClassLoader();
       System.out.println(classLoaderTest);//系统加载器
       //调用系统类加载的getParent():获取扩展类加载器
       ClassLoader parent = classLoaderTest.getParent();
       System.out.println(parent);//上一层(扩展类加载器)
         //调用扩展类加载器的getParent():无法获取引导类加载器
       //引导类加载器主要负责加载java的核心类库,无法加载自定义类的
       ClassLoader classLoader=parent.getParent();
       System.out.println(classLoader);//看下还能不能往上找

       ClassLoader classLoader1 = String.class.getClassLoader();
       System.out.println(classLoader1);

       ClassLoaderTest classLoaderTest1=new ClassLoaderTest();
       try {
           classLoaderTest1.test2();
      } catch (Exception e) {
           throw new RuntimeException(e);
      }
  }
   /*
   Properties:用来读取配置文件
    */
   public void test2() throws Exception {
       Properties prot=new Properties();
       //此时的文件默认在当前的module下
       //读取配置文件方式1
//       FileInputStream inputStream=new FileInputStream("jdbc.properties");
//       prot.load(inputStream);

       //读取配置文件方式2:使用ClassLoader
       //配置文件默认识别为Module的src下
       ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
       InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties");
             prot.load(resourceAsStream);

       String user = prot.getProperty("user");
       String password = prot.getProperty("password");
       System.out.println(user+"="+password);
  }
}
package Reflection1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

public class ClassLoaderTest
{
   public static void main(String[] args)
  {
       //对于自定义类,使用系统类加载器进行加载
       ClassLoader classLoaderTest=ClassLoaderTest.class.getClassLoader();
       System.out.println(classLoaderTest);//系统加载器
       //调用系统类加载的getParent():获取扩展类加载器
       ClassLoader parent = classLoaderTest.getParent();
       System.out.println(parent);//上一层(扩展类加载器)
         //调用扩展类加载器的getParent():无法获取引导类加载器
       //引导类加载器主要负责加载java的核心类库,无法加载自定义类的
       ClassLoader classLoader=parent.getParent();
       System.out.println(classLoader);//看下还能不能往上找

       ClassLoader classLoader1 = String.class.getClassLoader();
       System.out.println(classLoader1);

       ClassLoaderTest classLoaderTest1=new ClassLoaderTest();
       try {
           classLoaderTest1.test2();
      } catch (Exception e) {
           throw new RuntimeException(e);
      }
  }
   /*
   Properties:用来读取配置文件
    */
   public void test2() throws Exception {
       Properties prot=new Properties();
       //此时的文件默认在当前的module下
       //读取配置文件方式1
//       FileInputStream inputStream=new FileInputStream("jdbc.properties");
//       prot.load(inputStream);

       //读取配置文件方式2:使用ClassLoader
       //配置文件默认识别为Module的src下
       ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
       InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties");
             prot.load(resourceAsStream);

       String user = prot.getProperty("user");
       String password = prot.getProperty("password");
       System.out.println(user+"="+password);
  }
}
user=zf;
password=abc1234
 
posted @ 2022-08-03 17:52  zjw_rp  阅读(92)  评论(0)    收藏  举报