I am using guice and saw an example that is using binder().requireExplicitBindings()
here.
The example looks like that:
Injector injector = Guice.createInjector(new SandwichModule(), new AbstractModule() {
@Override
protected void configure() {
binder().requireExplicitBindings();
bind(GuiceFilter.class);
}
});
That cause exceptions such as
com.google.inject.ConfigurationException: Guice configuration errors:
1) Explicit bindings are required and ...
Is it mandatory to use it, or just recommended? and if its only recommended I just wonder why to use it?
It is neither mandatory (then this would be a default setting without the need of activation) nor required. Normally, when using guice, you want to have all the "magic" to help you glueing your application together. So the default behavior is: do not require explicitBindings. But every once in a while, you notice that the guice automatisms get in your way. In that rare occasions, you would use the require switch. I did not read the whole article you provided, so I cannot tell if they are dealing with one of these "special cases" or if they just use a flag without really considering why. Anyhow, If you write your binding modules, just leave it out.
I think this switch could be quite useful to point you faster to programming errors where you wrongly annotated something as injectable that should not be injected, like here:
@Inject
private SomeClass someInstance = new SomeClass(12);
If SomeClass now also has a public no-argument constructor, Guice would just overwrite the specific instance without saying a thing.
However, the usefulness of automatic injection usually weights much more than this, so for this specific example you just have to be careful to not "inject everything" by accident :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With