Java ClassLoader for Working with Instance Classes

As we know, loading the java class works like in the picture below.

enter image description here

When we have the concept of a plugin in our application (for example, on application servers), we sometimes need some classes to be loaded into the instance classloader rather than the parent so that every new instance (plugin, webapp, or whatever) loads this specific class not delegating the parent ...

For example, Log4j classes, we have to load them for each instance.

So, to do this, the best approach that came to my mind is to write a custom classloader that will take a list of class names that should be delegated to the parent classloader (ideally, we want the instances to be in complete isolation).

Thus, an application that will load other "instances" will use this custom classloader when loading those "instances" ...

Is there an implementation of such a classloader that solves this problem? (considering that we don't want to know what OSGi is and we are working with pure java without frameworks, etc.)

My searches end up pointing to some framework or some specific web container solutions, but this is a pretty simple task solved by one class, I would like to know if something is missing before I start implementing it.

Update (dig deeper): Assuming there is a bootstrap loaded class that has static state that can be shared between instances (and we really really want to make sure the instances are completely isolated), now this class is obviously not included in our the classpath, but it's loaded, and if we copy it over instead of repeating it, we achieve the isolation we need.

So, do we have the concept of copying or cloning a class from one classloader to another?

+3


source to share





All Articles