I'm using Java's ServiceLoader
to load plugins into my app. The plugins are provided as JARs on the classpath when starting the app. Now, if these plugins contain classes or resources with the same name, there may be conflicts.
Thus, what's the proper way to load plugins via ServiceLoader
in an isolated way, so that
- plugin
A
cannot "see" the classes / resources of pluginB
after loading them into the app - the app can use the same dependency as a plugin does, but in a different version (i.e the plugin's dependencies do not leak into the app)
Is it as easy as passing a custom classloader (that's based on the app's classloader) to ServiceLoader.load()
? Can anyone provide a concrete example?