Reputation: 495
Java ClassLoader delegation model means:
When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.
I have seen the question.Generally we should use it. But I want to know more. Is there any good reasons that we should not use it?
Upvotes: 2
Views: 123
Reputation: 29999
The answer of the linked question and the Tomcat classloader documentation that is quoted there seems to provide a good use case: web containers like Tomcat, that run isolated web applications.
You "generally" want to the use delegation model to avoid different parts of your application to load classes with different class loaders, because otherwise classes would be loaded multiple times but would not be compatible with each other. This is useful for single applications - but web containers and applications servers can run multiple applications where you actually want applications to be able to load their own versions of some classes.
For example, with Tomcat each of the running web applications might use a different set of libraries or different versions of the same library. There are a few shared repositories for libraries that should be available for all web application and also an application-specific repository for each web application.
Each repository has its own class loader. Here is Tomcat's class loader tree, with parent class loaders at the top:
Bootstrap
|
System
|
Common
/ \
Webapp1 Webapp2 ...
The application specific repositories should have priority, because they may contain other library versions than the shared repositories.
Upvotes: 3