Created
October 17, 2014 14:13
-
-
Save pjagielski/a48852c687efd22e5a40 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.api.Factory; | |
import org.glassfish.hk2.utilities.Binder; | |
import org.glassfish.hk2.utilities.binding.ServiceBindingBuilder; | |
import org.glassfish.jersey.internal.inject.Injections; | |
@SuppressWarnings("unchecked") | |
public class InstantFactoryBinder { | |
ServiceBindingBuilder builder; | |
InstantFactoryBinder(Factory<?> factory) { | |
this.builder = Injections.newFactoryBinder(factory); | |
} | |
public static InstantFactoryBinder bindFactory(Factory<?> factory) { | |
return new InstantFactoryBinder(factory); | |
} | |
public InstantFactoryBinder to(Class<?> clazz) { | |
builder.to(clazz); | |
return this; | |
} | |
public InstantFactoryBinder in(Class<?> scope) { | |
builder.in(scope); | |
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