Play什么时候加载application.conf?

时间:2014-02-25 09:14:19

标签: scala playframework config

执行application.conf中的代码时是否已加载Global.scala?我问,因为我试图从Global.scala读取一些配置项,我总是得到None。有没有解决方法?

2 个答案:

答案 0 :(得分:1)

在Java中,它已经可用beforeStart(Application app)

public class Global extends GlobalSettings {
     public void beforeStart(Application app) {
        String secret = Play.application().configuration().getString("application.secret");
        play.Logger.debug("Before start secret is: " + secret);
        super.beforeStart(app);
    }   
}

因为需要配置数据库连接,很可能是Scala works the same way(无法检查)

答案 1 :(得分:1)

下面是如何在加载之后但在应用程序实际启动之前读取配置:

import play.api.{Configuration, Mode}
import play.api.GlobalSettings
import java.io.File
import utils.apidocs.InfoHelper

object Global extends GlobalSettings {

  override def onLoadConfig(
    config: Configuration,
    path: File, classloader:
    ClassLoader,
    mode: Mode.Mode): Configuration = {

    InfoHelper.loadApiInfo(config)
    config
  }
}

以下仅为了您的信息,是InfoHelper.loadApiInfo的来源 - 它只是加载 Swagger UI 的API信息:

package utils.apidocs

import play.api.Configuration
import com.wordnik.swagger.config._
import com.wordnik.swagger.model._

object InfoHelper {

  def loadApiInfo(config: Configuration) = {
    config.getString("application.name").map { appName =>
    config.getString("application.domain").map { appDomain =>
    config.getString("application.emails.apiteam").map { contact =>
      val apiInfo = ApiInfo(
        title = s"$appName API",
        description = s"""
          Fantastic application that makes you smile.  You can find our
          more about $appName at <a href="//$appDomain">$appDomain</a>.
        """,
        termsOfServiceUrl = s"//$appDomain/terms",
        contact = contact,
        license = s"$appName Subscription and Services Agreement",
        licenseUrl = s"//$appDomain/license"
      )

      ConfigFactory.config.info = Some(apiInfo)
    }}}
  }
}

我希望它有所帮助。