-
Notifications
You must be signed in to change notification settings - Fork 1.7k
LinkedBindings
Googler edited this page Jun 2, 2020
·
8 revisions
Linked bindings map a type to its implementation. This example maps the
interface TransactionLog
to the implementation DatabaseTransactionLog
:
public class BillingModule extends AbstractModule {
@Provides
TransactionLog provideTransactionLog(DatabaseTransactionLog impl) {
return impl;
}
}
Now, when you call injector.getInstance(TransactionLog.class)
, or when the
injector encounters a dependency on TransactionLog
, it will use a
DatabaseTransactionLog
. Link from a type to any of its subtypes, such as an
implementing class or an extending class. You can even link the concrete
DatabaseTransactionLog
class to a subclass:
bind(DatabaseTransactionLog.class).to(MySqlDatabaseTransactionLog.class);
Linked bindings can also be chained:
public class BillingModule extends AbstractModule {
@Provides
TransactionLog provideTransactionLog(DatabaseTransactionLog databaseTransactionLog) {
return databaseTransactionLog;
}
@Provides
DatabaseTransactionLog provideDatabaseTransactionLog(MySqlDatabaseTransactionLog impl) {
return impl;
}
}
In this case, when a TransactionLog
is requested, the injector will return a
MySqlDatabaseTransactionLog
.
-
User's Guide
-
Integration
-
Extensions
-
Internals
-
Releases
-
Community