Skip to content

Instantly share code, notes, and snippets.

@pjagielski
Created October 17, 2014 14:13
Show Gist options
  • Save pjagielski/27e9bae96d8a4237fb28 to your computer and use it in GitHub Desktop.
Save pjagielski/27e9bae96d8a4237fb28 to your computer and use it in GitHub Desktop.
package pl.allegro.tech.hermes.common.di;
import org.glassfish.hk2.api.DynamicConfiguration;
import org.glassfish.hk2.utilities.Binder;
import org.glassfish.hk2.utilities.binding.ScopedBindingBuilder;
import org.glassfish.jersey.internal.inject.Injections;
@SuppressWarnings("unchecked")
public class InstantBinder {
ScopedBindingBuilder builder;
InstantBinder(Object instance) {
this.builder = Injections.newBinder(instance);
}
public static <T> InstantBinder bind(T instance) {
return new InstantBinder(instance);
}
public InstantBinder named(String name) {
builder.named(name);
return this;
}
public InstantBinder to(Class<?> clazz) {
builder.to(clazz);
return this;
}
public Binder build() {
return new Binder() {
@Override
public void bind(DynamicConfiguration config) {
Injections.addBinding(builder, config);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment