Say we want to provide a plugin interface called org.example.plugins.PluginService
. We then provide an implementation of this service in the class org.example.plugins.impl.ExamplePlugin
.
If we want to have some sort of plugin mechanism, we could create a JAR file, that contains the implementation. This JAR file must also contain the file META-INF/services/org.example.plugins.PluginService
. This file must contain one line
org.example.plugins.impl.ExamplePlugin
to enable the ServiceLoader
to find the implementation. If that JAR file is in the build path, you can load the plugin by calling
Iterator<PluginService> it = ServiceLoader.load(PluginService.class).iterator();
That iterator will give you access too all plugins that are found by the ServiceLoader
.
http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html