Sunday, November 13, 2016

ClassLoader

ClassLoader:
Java class loader is a class which used to load classes at runtime. ClassLoader in Java works on three principle:
  • 1.       Delegation principle: Responsibility of loading class is of parent classloader. A classloader will load class only if parent is not able to find or load class.
  • 2.       Visibility principle: Child class loader can see all the classes loaded by parent ClassLoader, but parent class loader can not see classes loaded by child.
  • 3.       Uniqueness principle: A class should be loaded exactly once. This is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by parent.


Types of Classloader:
There are three default class loader used in Java:
  • 1.       Bootstrap ClassLoader/Primordial ClassLoader is responsible for loading standard JDK class files from rt.jar and it is parent of all class loaders in Java. Bootstrap class loader don't have any parents, if you call String.class.getClassLoader() it will return null and any code based on that may throw NullPointerException in Java.
  • 2.       Extension ClassLoader delegates class loading request to its parent, Bootstrap and if unsuccessful, loads class form jre/lib/ext directory or any other directory pointed by java.ext.dirs system property. Extension ClassLoader in JVM is implemented by sun.misc.Launcher$ExtClassLoader.
  • 3.       Application class loader is responsible for loading application specific classes from CLASSPATH environment variable, -classpath or -cp command line option, Class-Path attribute of Manifest file inside JAR. Application class loader is a child of Extension ClassLoader and its implemented by sun.misc.Launcher$AppClassLoader class.


Except Bootstrap class loader, which is implemented in native language mostly in C, all Java class loaders are implemented using java.lang.ClassLoader.

Explicitly load a class:
Java provides API to explicitly load a class by
  • 1.       Class.forName(classname)
  • 2.       Class.forName(classname, initialized, classloader)


Uses:
  • 1.       J2EE uses multiple class loaders to load class from different location e.g. classes from WAR file will be loaded by Web-app ClassLoader while classes bundled in EJB-JAR is loaded by another class loader.
  • 2.       Some web server also supports hot deploy functionality which is implemented using ClassLoader.
  • 3.       You can also use ClassLoader to load classes from database or any other persistent store.


String.class.getClassLoader() returns null because this class is loaded by bootstrap classLoader which is not implemented in java , it's either implemented in c or c++ so there is no reference for it that's why it returns null.
Why write a Custom ClassLoader in Java
If you are expecting a class at the runtime or from FTP server or via third party web service at the time of loading the class then you have to extend the existing class loader.

How does Java ClassLoader Work
  • 1.       When JVM requests for a class, it invokes loadClass function of the ClassLoader by passing the fully classified name of the Class.
  • 2.       loadClass function calls for findLoadedClass() method to check that the class has been already loaded or not. It’s required to avoid loading the class multiple times.
  • 3.       If the Class is not already loaded then it will delegate the request to parent ClassLoader to load the class.
  • 4.       If the parent ClassLoader is not finding the Class then it will invoke findClass() method to look for the classes in the file system.


Custom ClassLoader in Java

By extending ClassLoader class and overriding loadClass(String name) 

SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...