How to use ClassLoader to load resources such as classes, files, and Web resources in Java

Source: Internet
Author: User
Tags thread

ClassLoader mainly serves the request of the class, and when the JVM needs a class, it requires the class according to the name to ClassLoader, and then the class object of the classes is returned by ClassLoader.

ClassLoader is responsible for loading all the resources (Class, files, pictures, streams of bytes from the network, etc.) in the system and loading the resources into the JVM through ClassLoader. Each class has a reference that points to its own classloader.

1. Several methods of obtaining ClassLoader

ClassLoader can be obtained through the following 3 methods:

This.getClass.getClassLoader ();  Use the current class's ClassLoader   
      
Thread.CurrentThread (). Getcontextclassloader ();  ClassLoader Classloader.getsystemclassloader () using the current thread   
      
;  Use the system ClassLoader, which is the ClassLoader used by the system's entry point.

Note: System ClassLoader is not the same as root classloader. The system ClassLoader under the JVM is typically the app ClassLoader.

2. Several methods of loading resources with ClassLoader

All resources are loaded into the JVM through ClassLoader, so you can certainly use ClassLoader when loading resources, but there are other ways to load them for different resources, for example, for classes can be directly new, for files can be directly Io, and so on.

2.1 Loading mode of class

Assuming that class A and class b,a need to instantiate B in their methods, there are 3 possible methods for loading classes. For loading a class, the user needs to know the full name of Class B (including the package name, such as "com.alexia.b")

1. Use Class static method Class.forName

Class cls = Class.forName ("com.alexia.b");  
 b b = (b) cls.newinstance ();

2. Use of ClassLoader

/* Step 1. Get ClassLoader *  
/ClassLoader cl = This.getClass.getClassLoader ()  ; How to get ClassLoader reference 1/  
      
* Step 2. Load  
the class/Class CLS = Cl.loadclass ("com.alexia.b")////////Use the ClassLoader obtained in the first step to load the B/  
         
* Steps 3. New instance */< C8/>b B = (b) cls.newinstance (); A class with B gets an instance of B

3. Direct NEW

b b = new B ();

Note: One might think that we would choose the simplest 3rd way to load the class, and that the first two ways are completely redundant.

In fact, the direct new way is also limited, for example, the simplest case: Java in the package name of the class how to refer to the default package class? This is, of course, because a class with a package name cannot directly use new to refer to a class in the default package. The answer is to use the reflection mechanism, even if the class is loaded in the first way (see here for details). Also, the example of creating a class with new () and newinstance () is different, and the main difference is simply described as follows:

From the JVM's point of view, when we use the keyword new to create a class, this class can be not loaded. However, when using the Newinstance () method, you must ensure that:

(1) This class has been loaded;

(2) This class has been linked (that is, allocating storage space for the static domain and parsing all references to other classes created by the class if necessary). The static method forname (), which completes the two steps above, is done by calling the boot class loader, the loader that loads the JAVAAPI.

As you can see, newinstance () actually breaks down the new method into two steps, which is to first call the class load method to load a class and then instantiate it. The benefits of this step-by-step are obvious. We can get more flexibility when calling the static Load method of class forname, and provide a means of decoupling.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.