Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CommonModule extends AbstractModule {
- private final Config config;
- public CommonModule(Config config) {
- this.config = config;
- }
- @Override
- protected void configure() {
- this.bind(Morphia.class).toProvider(MorphiaProvider.class).asEagerSingleton();
- }
- @Provides
- public MongoClient provideMongoClient() {
- ServerAddress serverAddress = new ServerAddress(this.config.databaseHost(), this.config.databasePort());
- MongoCredential credential = MongoCredential.createCredential(this.config.databaseUser(), this.config.databaseName(), this.config.databasePassword().toCharArray());
- return new MongoClient(serverAddress, Collections.singletonList(credential));
- }
- @Provides
- @Named(Constants.DATASTORE)
- public Datastore provideDatastore(MongoClient mongoClient, Morphia morphia) {
- Datastore datastore = morphia.createDatastore(mongoClient, this.config.databaseName());
- datastore.ensureIndexes();
- return datastore;
- }
- @Provides
- public Config provideConfig() {
- return this.config;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment