- bootstrap class loader: loads Java's core classes, the java.* packages, the path is found on sun.boot.class.path sytem property, and usually it loads rt.jar and i18n.jar files, this classloader also loads the main class and all other classes that are referenced;
- extensions class loader: loads classes from the ext folder located in JAVA_HOME/jre/lib/ext, the path is found on java.ext.dirs system property;
- system class loader: loads classes that are available on paths specified by the system property java.class.path, the default value is the current directory, but you can change it by using the -classpath or -cp command-line options, or setting the CLASSPATH environment variable.
Delegation model
The class loaders are linked in a hierarchical structure. When asked to load a class, a class loader first asks its parent (class loader that loaded it) to load the class. If the parent can't load that class, the class loader would try to load it itself. If the parent succeeded, that class is returned to the class loader. On creation of a class loader instance, we can use it's constructor to set the parent.
When a class loader tries to load a class, it delegates to its parent and the parent does the same until the bootstrap class loader.
Role of the Thread context ClassLoader and how it can be used
The context ClassLoader is provided when creating the thread for use by code running in this thread when loading classes and resources.It was introduced in Java 1.2 to enable frameworks running in application servers to access the right class loader for loading application classes.
The need of context class loader came because of the visibility problem in classpath. For example, say that you have a server (Tomcat), the framework jar would need to be placed in WEB-INF/lib and not on the system class path or in a server class path such as TOMCAT_HOME/common/lib.
The process of loading classes with Class.forName() Create an instance of the class by invoking the class.newInstance() method on the class object after dynamic loading.
Example:
String className = "ro.blogspot.UserDao"; Class cls = Class.forName(className); UserDao userDao = (UserDao)cls.newInstance(); Listusers = userDao.getAllUsers();
No comments:
Post a Comment