Created
October 17, 2014 14:13
-
-
Save pjagielski/27e9bae96d8a4237fb28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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