Skip to content

Instantly share code, notes, and snippets.

@igmar
Created September 25, 2017 19:09
Embed
What would you like to do?
Custom config Provider with configuration
public final Foo {
@Inject
private Bar bar;
public Foo(String config1) {
// Do something
}
}
public class FooProviderModule extends AbstractModule {
@Override
protected void configure() {
bind(Foo.class).toProvider(FooProvider.class).in(Singleton.class);
}
}
public class FooProvider extends Provider<Foo> {
private final Foo fooInstance;
@Inject
public FooProvider(final com.google.inject.Injector injector, final FooConfig fooConfig) {
this.fooInstance = new Foo(fooConfig.value1);
// If Foo needs DI :
injector.injectMembers(this.fooInstance);
}
@Override
public Foo get() {
return this.fooInstance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment