Custom config Provider with configuration
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
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