Java custom class loading order


Idea 1: Break parental loading
1. The default class loading order in Java is the parent class delegation loading, but sometimes you need to customize the loading order, such as OSGI and ali's Pandora.

2. The basic implementation idea of ​​parent class entrusted loading. In loadClass, find the class in the parent. If there is no parent, use findBootstrapClassOrNull to call the native method. If it is not found, use findClass to find it.

3. Ordinary custom classLoader just inherits the loadClass method, and implements findClass to customize to load its own location or its own implemented class. However, if you want to break the parent class delegation mechanism to customize the front and back order to load classes, you must completely implement loadClass to control the loading order by yourself, such as ClassLoader that references each other between OSGI bundles to load.

Idea 2, use parents to load
1. The basic idea is to customize the classLoader, then set the custom classLoader as the parent of the extClassLoader, and then use the parents to load (remove the jdk class) other classes will load their own classes, preventing the class conflict issues.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326594336&siteId=291194637