好吧,我想通了。這對Java編程人員來說可能是顯而易見的,但我不是一個人,所以這就是我拼湊在一起的東西。
String mongo_url = System.getenv("MONGOHQ_URL");
// If env var not set, try reading from Java "system properties"
if (mongo_url == null) {
mongo_url = System.getProperty("MONGOHQ_URL");
}
MongoURI mongoURI = new MongoURI(mongo_url);
this.db = mongoURI.connectDB();
// Only authenticate if username or password provided
if (!"".equals(mongoURI.getUsername()) || mongoURI.getPassword().length > 0) {
Boolean success = this.db.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
if (!success) {
System.out.println("MongoDB Authentication failed");
return;
}
}
this.my_collection = db.getCollection("my_collection");
您可以從使用env vars切換到使用Java系統屬性,並且只要在Heroku上運行時將env vars傳遞到系統屬性。 – 2012-04-17 18:04:06
@JamesWard謝謝。我如何設置Java系統屬性? – 2012-04-17 18:14:38
這取決於你如何在Heroku上啓動你的應用程序。但是,它可能會被添加類似下面你Procfile:'-Dmongourl = $ {MONGOHQ_URL}' – 2012-04-18 09:28:21