Documentation / Setup / Guice
Provider
Create a Guice provider that programmatically creates a Database
using Database.builder().
import io.ebean.Database;
import com.google.inject.Provider;
public class DatabaseProvider implements Provider<Database> {
@Override
public Database get() {
return Database.builder()
.name("pg")
.loadFromProperties()
.defaultDatabase(true)
.build();
}
}
Module bind
In the Guice module bind the provider using eager singleton so that the
Database is created eagerly at startup.
// bind the provider as eager singleton
bind(Database.class).toProvider(DatabaseProvider.class).asEagerSingleton()
Inject and ActiveRecord
Prefer injecting Database into application code. In practice most applications
still keep register(true) and defaultDatabase(true) so that query beans,
Model and other default-database-based APIs can resolve the database, while application code
itself continues to use the injected Database instance.