A module to use Morphia/MongoDB with Play! framework 2.8.x
Add the repository:
resolvers += "auslides repo" at "https://github.com/auslides/repository/raw/master/maven/releases"
and add the following build dependency:
"org.auslides" %% "play-morphia-module" % "2.8.1"
morphia.db.host="127.0.0.1"
morphia.db.port="27017"
morphia.db.username=<username>
morphia.db.password=<password>
or
mongodb.uri="mongodb://username:password@localhost:27017/dbname"
For clustering:
morphia.db.seeds="127.0.0.1:27017"
morphia.db.dev.seeds="127.0.0.1:27017"
morphia.db.username=<username>
morphia.db.password=<password>
morphia.scan.packages=[com.package1,com.pakage2]
and/or
morphia.scan.classes=[com.my.Class1,com.my.Class2]
Play now supports Dependency Injection. There is a morphia module defined. You could inject it by
@Inject IMorphia morphia ;
in your component. This will use the configuration prefixed with 'morphia' in the application.conf. You could also use your own prefix name, for example:
play.modules.morphia.prefixes=[mymorphia,yourmorphia]
......
mymorphia.db.host="127.0.0.1"
.....
then injected it by:
@Inject @ConfigPrefix("mymorphia") IMorphia morphia ;
IMorphia Interface:
public interface IMorphia {
public Morphia underlying();
public Datastore ds(String dbName);
public Datastore ds();
public DB db();
public GridFS gridFs();
}
The password is default to plain text. If a encrypted password is used, you need to define a password decryptor by implementing IPasswordDecryptor interface:
` public class MyPasswordDecryptor implements IPasswordDecryptor {
@Override
public String decrypt(String encrypted) {
String decryptedPwd = ....
return decryptedPwd;
}
}
Then binding this implementation to IPasswordDecryptor in your module:
bind(IPasswordDecryptor.class).to(MyPasswordDecryptor.class)
see Providing custom bindings in Dependency Injection.
In application.conf, you should disable the default password decryptor module:
play.modules.disabled += "org.auslides.play.module.morphia.PasswordDecryptorModule"
see Excluding modules in Dependency Injection.