Reputation: 109
Is the any way to bind classes not via code:
bind(MessageService.class).to(FacebookService.class);
Injector injector = Guice.createInjector(new AppInjector());
But with some annotation?
Upvotes: 0
Views: 54
Reputation: 127781
Yes, there is a way to use annotations to specify bindings, it is explained in the Guice docs. An example from that page:
@ImplementedBy(PayPalCreditCardProcessor.class)
public interface CreditCardProcessor {
ChargeResult charge(String amount, CreditCard creditCard)
throws UnreachableException;
}
@ProvidedBy(DatabaseTransactionLogProvider.class)
public interface TransactionLog {
void logConnectException(UnreachableException e);
void logChargeResult(ChargeResult result);
}
@ImplementedBy
and @ProvidedBy
annotations allow to specify bindings implicitly.
However, I'd argue that this is not really a good way to define bindings. Explicitly defined bindings are more composable, and they are concentrated in modules hence they are easier to manage.
Upvotes: 2