samedi 4 avril 2015

Spring Boot configuration with Morphia?

I do not want to utilize the Spring DATA MongoDB support. I want to leverage the ORM for MongoDB called Morphia.


http://ift.tt/1jjv4kb


I want to configure the Morphia with Spring Boot. I want to externalize the configuration of Morphia in a way that it follows the Spring Boot philosophy.


I want to leverage the environment variables for the configuration of Morphia properties.


What would be the Spring Boot approach to achieve this ?


In a simple main program on would do following to get the Morhpia ORM working.



private Morphia morphia;
private MongoClient mongoClient;

morphia = new Morphia();
// Person is an entity object with Morphia annotations
morphia.map(Person.class);

// THESE properties MUST be read from environment variables in Spring BOOT.
final String host = "localhost";
final int port = 27017;

mongoClient = new MongoClient(host, port);

//Set database
// this instance would be autowired all data access classes
Datastore ds = morphia.createDatastore(mongoClient, "dataStoreInstanceId");

// this is how instance would be used in those data accesses classes
Person p = ds.find(Person.class, "username", "john").get();

Aucun commentaire:

Enregistrer un commentaire