Plugin installer¶
CoreInstallersBundle / PluginInstaller
Allows automatic gathering of multiple implementations of some interface into bindable set or map (dynamic plugins case).
Recognition¶
Detects classes annotated with guicey @Plugin
annotation and bind them into set or map using guice
multibindings mechanism.
Suppose you have plugin interface public interface PluginInterface
.
Annotate plugin implementations with @Plugin
:
@Plugin(PluginInterface.class)
public class PluginImpl1 implements PluginInterface
Now all implementations could be autowired as
@Inject Set<PluginInterface> plugins;
Warning
At least one implementation must be provided because otherwise guicey will not be able to register
Setpublic class MyModule extends AbstractModule {
@Override
protected configure() {
Multibinder.newSetBinder(binder(), PluginInterface.class);
}
}
Named plugins¶
Sometimes it's required to have named plugin mapping: to bind, Map
Most likely, you would use enum for keys:
public enum PluginKey {
FIRST, SECOND
}
Custom plugin annotation needs to be defined to use new keys:
@Plugin(PluginInterface.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyPlugin {
PluginKey value();
}
Note
Annotation itself is annotated with @Plugin
, defining target plugin interface.
Guicey will detect your custom annotation usage by analyzing its annotations.
Annotating plugin implementation:
@MyPlugin(PluginKey.FIRST)
public class PluginImpl1 implements PluginInterface
All plugins could be referenced as map:
@Inject Map<PluginKey, PluginInterface> plugins;
Note
It's not required to use enum as key. Any type could be set in your custom annotation. For example, string key could be used:
public @interface MyPlugin {
String value();
}
@MyPlugin("first")
public class PluginImpl1 implements PluginInterface
@Inject Map<String, PluginInterface> plugins;
Warning
As with simple plugin bindings, at least one plugin must be registered so guice could create map binding. Otherwise, you need to manually declare empty (default) plugins map binding:
MapBinder.newMapBinder(binder, keyType, pluginType);