beadledom-guice-dynamicbindings¶
Overview¶
Enables creation of multiple bindings for the same Java Type
to be used for different purposes. DynamicBindings
are namespaced to ensure that all bindings
for the same Type
are kept separate.
Namespacing may be achieved using either BindingAnnotation or Qualifier. Additionally Guice
provides a way to namespace bindings using BindingAnnotations. However, it is not always possible
to provide or retrieve bindings at compile time when using BindingAnnotations
. This module also
provides a way to retrieve bindings for a Type
given a specific Annotation
at runtime,
assuming the relationship between the Annotation
and Type
was created ahead of time.
Download¶
Download using Maven¶
<dependencies>
...
<dependency>
<groupId>com.cerner.beadledom</groupId>
<artifactId>beadledom-guice-dynamicbindings</artifactId>
<version>[Insert latest version]</version>
</dependency>
...
</dependencies>
Usage¶
- Binding a dynamic provider
public Mymodule extends AnnotatedModule {
...
public Mymodule(Class<? extends Annotation> bindingAnnotation) {
super(bindingAnnotation);
}
@Override
protected void configure() {
...
bind(MyClass.class).annotatedWith(getBindingAnnotation).toInstance(new MyClass());
bindDynamicProvider(MyClass.class);
...
}
}
- Retrieving the dynamically bound provider
public Injector injector = Guice.createInjector(new MyModule())
TypeLiteral typeLiteral = new TypeLiteral<DynamicBindingProvider<MyClass>>() {}
Provider provider = injector.getInstance(Key.get(typeLiteral))
provider.get(MyBindingAnnotation.class) // instance of the MyClass bound to MyBindingAnnotation