Just-in-time Bindings
When the injector needs an instance of a type, it needs a binding. The bindings in a modules are called explicit bindings, and the injector uses them whenever they're available. If a type is needed but there isn't an explicit binding, the injector will attempt to create a Just-In-Time binding. These are also known as JIT bindings and implicit bindings.
Eligible Constructors
Guice can create bindings for concrete types by using the type's injectable constructor. This is either a public, no-arguments constructor, or a constructor with the @Inject annotation:
public class PayPalCreditCardProcessor implements CreditCardProcessor {
private final String apiKey;
@Inject
public PayPalCreditCardProcessor(@Named("PayPal API key") String apiKey) {
this.apiKey = apiKey;
}Guice will not construct nested classes unless they have the static modifier. Inner classes have an implicit reference to their enclosing class that cannot be injected.
@ImplementedBy
Annotate types tell the injector what their default implementation type is. The @ImplementedBy annotation acts like a linked binding, specifying the subtype to use when building a type.
@ImplementedBy(PayPalCreditCardProcessor.class)
public interface CreditCardProcessor {
ChargeResult charge(String amount, CreditCard creditCard)
throws UnreachableException;
}The above annotation is equivalent to the following bind() statement:
bind(CreditCardProcessor.class).to(PayPalCreditCardProcessor.class);
If a type is in both a bind() statement (as the first argument) and has the @ImplementedBy annotation, the bind() statement is used. The annotation suggests a default implementation that can be overridden with a binding. Use @ImplementedBy carefully; it adds a compile-time dependency from the interface to its implementation.
@ProvidedBy
@ProvidedBy tells the injector about a Provider class that produces instances:
@ProvidedBy(DatabaseTransactionLogProvider.class)
public interface TransactionLog {
void logConnectException(UnreachableException e);
void logChargeResult(ChargeResult result);
}The annotation is equivalent to a toProvider() binding:
bind(TransactionLog.class)
.toProvider(DatabaseTransactionLogProvider.class);Like @ImplementedBy, if the type is annotated and used in a bind() statement, the bind() statement will be used.
Hello! In a project, I try tu use guice to provide instance of a implementation of a interface. There's no explicit bindings but a implementation with public no-args constructor exists. Guice throws a ConfigurationException?. Here some details:
com.google.inject.ConfigurationException?: Guice configuration errors:
1) No implementation for com.test.IHello was bound.
But I have this class:
public Hello implements IHello{
} but guice is not able to create instance. What's wrong in my code?
Ok, I've tried to go deeper to understand my problem and Guice seems to disable JIT bindings in options? Do we have a way to enable it? Thanks
I seem to have the same problem, however I are using private modules and childinjectors.. This should go to the forum
How to set a scope such as Singleton for class X with annotation @ProvidedBy?(Y) ?
Thank you.
@Olivier: Guice doesn't create Just-In-Time bindings for interfaces, even if there is only one implementation of that interface on the classpath.
Just-In-Time bindings only work for classes.
No - if IHello includes @ImplementedBy?(Hello.class) then Guice should pick it up. But only if that signal is present.