Note that there are some explanatory texts on larger screens.

plurals
  1. POJava ServiceLoader with multiple Classloaders
    primarykey
    data
    text
    <p>What are the best practices for using <a href="http://download.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html" rel="nofollow noreferrer">ServiceLoader</a> in an Environment with multiple ClassLoaders? The documentation recommends to create and save a single service instance at initialization:</p> <pre><code>private static ServiceLoader&lt;CodecSet&gt; codecSetLoader = ServiceLoader.load(CodecSet.class); </code></pre> <p>This would initialize the ServiceLoader using the current context classloader. Now suppose this snippet is contained in a class loaded using a shared classloader in a web container and multiple web applications want to define their own service implementations. These would not get picked up in the above code, it might even be possible that the loader gets initialized using the first webapps context classloader and provide the wrong implementation to other users.</p> <p>Always creating a new ServiceLoader seems wasteful performance wise since it has to enumerate and parse service files each time. <strong>Edit:</strong> This can even be a big performance problem as shown in <a href="https://stackoverflow.com/questions/6340802/java-xpath-apache-jaxp-implementation-performance/6341739#6341739">this answer regarding java's XPath implementation</a>.</p> <p>How do other libraries handle this? Do they cache the implementations per classloader, do they reparse their configuration everytime or do they simply ignore this problem and only work for one classloader?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload