【发布时间】:2015-06-04 14:37:16
【问题描述】:
我不想使用 Spring DATA MongoDB 支持。 我想利用名为 Morphia 的 MongoDB 的 ORM。
https://github.com/mongodb/morphia
我想用 Spring Boot 配置 Morphia。我想以遵循 Spring Boot 理念的方式外部化 Morphia 的配置。
我想利用环境变量来配置 Morphia 属性。
实现这一目标的 Spring Boot 方法是什么?
在一个简单的主程序中,将执行以下操作以使 Morhpia ORM 正常工作。
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();
【问题讨论】:
标签: java spring mongodb spring-boot morphia