更改 application.conf 运行时?

adi*_*dis 5 properties playframework

我想在运行时为我的 Play 项目设置数据库连接。我知道我可以使用以下代码设置属性运行时:

@OnApplicationStart public class Bootstrap extends Job
{
   @Override public void doJob()
   {
     // now set the values in the properties file
     Play.configuration.setProperty("db.driver", dbDriver);
     Play.configuration.setProperty("db.url", dbUrl);
     Play.configuration.setProperty("db.user", dbUsername);
     Play.configuration.setProperty("db.pass", dbPassword);
   }
}
Run Code Online (Sandbox Code Playgroud)

但是当执行上面的代码时,文件实际上并没有改变,我认为只是在内存中。

如何设置数据库属性并强制播放!使用此属性以连接到正确的数据库 onApplicationStart?

谢谢!

更新 2012-01-29

可以通过插件解决。在这个插件中,我必须覆盖 onConfigurationRead()并将属性应用于当时的配置文件。一旦我有时间,我会尝试发布一些代码。

Gel*_*Luo 2

当您更改属性时,数据库插件已经初始化。您需要编写一个插件并覆盖 onConfigurationRead() 方法,然后将新设置放在那里。Paly 的 dbplugin 稍后会初始化。