Java Se: Custom ClassLoader and seclassloader

Source: Internet
Author: User

Java Se: Custom ClassLoader and seclassloader

 

How does JVM know the classes in the java. lang package? How does JVM know the classes in our applications? There is a class in our application, but the JVM throws ClassNotFoundException. Why? XxxImpl class has implemented interface Xxx, But it throws XxxImpl does not extend from Xxx. Why? When type conversion is used, aa. bb. cc. XXX can not cast to aa. bb. cc may be thrown. Why? And so on. It's actually because of ClassLoader.

People who know reflection know that every Class has a Class object corresponding to it. So where does this Class object come from?

 

For more information about the Class, see:

http://blog.csdn.net/irelandken/article/details/7048817http://tyrion.iteye.com/blog/1958814http://www.blogjava.net/lihuaxajh/articles/94371.htmlhttp://www.javaworld.com/article/2077332/core-java/get-a-load-of-that-name.htmlhttp://www.javaworld.com/article/2075796/java-platform/java-101--class-and-object-initialization.html

 

My understanding of ClassLoader

The following is my understanding after reading these blogs and articles and customizing a ClassLoader for testing:

 

1) when a ClassLoader loads a class, it does not load all classes in the class, But dynamically loads the class according to the class to be used at runtime, that is, loading as needed.

2) When ClassLoader loads a class, it also loads its parent class (including interfaces)

3) When Class A is running, other classes (Class B, Class C, and Class D) to be used in the class are loaded by the loader of Class A by default.

4) when the type is forced conversion, first determine whether it is the same class loader. If not, the conversion cannot be performed. It seems that this process is not followed when an object A is converted to A class (the parent class or interface of A) loaded by BootstrapCLassLoader.

5) Thread # contextClassLoader can be used to switch the class loader.

6) by default, the context loader of the thread is the same as the context loader of the parent thread.

7) Class. forName ("xxx") and Xxx. class loaders use the Class loaders of the current class.

 

The following is the code list of the custom Class Loader:

 

Here is a simple description of these classes:

StringUtil. java

 

There are only two methods in StringUtil to determine the null value of a string. Writing this class is unnecessary, but you can see the effect in the test.

DebugUtil. java

 

This is just to output some information.

Person. java

 

This is a typical Java Bean and there is nothing to say.

ClassLoaderTestRunner. java

 

This is a business class. The Code is also very simple. It just loads the Person class, creates a Person object, and outputs some information.

This class can be tested either by using the system's default Class Loader (main method) or by using a custom class loader.

 

Custom ClassLoader

Currently, two common loading mechanisms are available: the delegated loading mechanism and the subclass loading priority mechanism.

1) process of the entrusted loading mechanism loadClass:

(1) Determine whether the class has been loaded

(2) If it is not loaded, the parent loader of the current class loader is loaded, that is, the loadClass () method is executed.

(3) If all parent loaders of the current class loader are not loaded, the current loader will call findClass () for loading.

 

2) process of the subclass priority Loader:

(1) Determine whether the class has been loaded

(2) The current class loader loads the class directly.

(3) The parent class loading process is not followed until it is loaded.

 

In JDK, the default loading method is 1), that is, the delegated loading mechanism.

 

The custom Class Loader generally overwrites loadClass and findClass.

 

The delegate field is used to specify whether the default delegated loading mechanism is used.

 

This is a custom method for loading classes. The process is:

1) Determine whether the loader has loaded the class to avoid repeated loading.

2) If delegate = true, the default loading method is used for delegation loading.

3) If delegate = false, exclude protected classes and use the following custom findClass to load classes. If an exception occurs during the above process, the default loading method will still be used.

 

 

FindClass loads and defines a Class object based on the Class name. This method is very rough, just to test ClassLoader.

 

 

Next we can use ClassLoaderTest. java to test:

 

A brief description of this test:

1) related classes in this test, such as String, UrlClassLoader, ClassLoader, Object, Thread, Runnable, Exception, etc., are all by default SystemClassLoader or its parent class (ExtClassLoader, BootstrapClassLoader). Currently, the ClassLoaderTest loader is SystemClassLoader.

2) when loading the ClassLoaderTestRunner class, use the custom Class Loader (that is, the sub-loader of SystemClassLoader ). When the ClassLoaderTestRunner class is loaded, its parent class, that is, the Object class and the Runable interface, will also be loaded.

 

3) The test execution process:

(1) create a custom loader instance and specify its parent loader as the system loader.

(2) Load class ClassLoaderTestRunner.

(3) Start a thread to execute related tasks.

 

4) As mentioned above, if the Object task = myAppClassLoader. loadClass (classFullName). newInstance ();

Changed:

ClassLoaderRunner task = (ClassLoaderRunner) myAppClassLoader. loadClass (classFullName). newInstance (); an error occurs.

ClassLoaderRunner is loaded by the system loader, and myAppClassLoader is a custom loader. Therefore, the two types cannot be converted.

If you directly use the custom class loader in this class to load the Person class, an error will also occur.

 

 

If you are interested, you can analyze the thread t execution process based on the content I summarized above, which classes are loaded by custom class loaders, which are loaded by system loaders, and what are the results of the preceding task execution?

 

If you have any questions, refer:

Reference

Http://blog.csdn.net/irelandken/article/details/7048817

Http://tyrion.iteye.com/blog/1958814

Http://www.blogjava.net/lihuaxajh/articles/94371.html

Http://www.javaworld.com/article/2077332/core-java/get-a-load-of-that-name.html

Http://www.javaworld.com/article/2075796/java-platform/java-101--class-and-object-initialization.html

 

Related Article

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.