【问题标题】:Play Framework 2.1: Overriding configuration file programmatically in Global settingsPlay Framework 2.1:在全局设置中以编程方式覆盖配置文件
【发布时间】:2013-10-04 23:21:14
【问题描述】:

我正在开发一个多租户 Play Framework 2.1 应用程序。我打算重写 GlobalSettings 类的 onRequest 方法,以根据请求的子域加载和设置自定义配置。问题是,我看不出这在 Play 2.x 中是如何实现的。

我可以在启动服务器时在命令行中覆盖系统属性,但是如何在 Java 代码中为每个请求以编程方式执行此操作?

代码看起来像这样(我假设):

@Override
public play.mvc.Action onRequest(Request request, Method actionMethod) {
    //Look up configuration settings in Cache based on request subdomain
    //(i.e. Cache.get("subdomain.conf"))
    //if not in cache: 
           //load appropriate configuration file for this subdomain (java.io.File)
           //set new configuration from file for this request
           //cache the configuration for future use in a new thread
   //else
           //set configuration from cache for this request
   return super.onRequest(request, actionMethod);
  }

}

查找 URL 和获取/设置缓存很容易,但我不知道如何以编程方式为 Play Framework 2.1 设置新配置,并且文档对此类内容略知一二。

有什么想法吗?有人知道更好、更有效的方法吗?

【问题讨论】:

  • 玩!使用 TypeSafe 配置库。你可以看看它(并且可能会找到你要找的东西):github.com/typesafehub/config
  • 我去看看,谢谢。

标签: java playframework playframework-2.1


【解决方案1】:

因此,以一种迂回的方式,我使用 Scala Global 创建了多租户 Play 应用程序的基础。使用过滤器可能有一种更有效的方法来实现这一点,但我发现这似乎到目前为止有效。这似乎在 Java 中实现起来并不容易。

我没有使用配置文件,而是使用了数据库。我认为使用键值缓存会更有效,但目前这似乎可行。

在 Global.scala 中:

object Global extends GlobalSettings {

  override def onRouteRequest(request: RequestHeader): Option[Handler] = {
    if (request.session.get("site").isEmpty){
      val id = models.Site.getSiteIDFromURL(request.host)
      request.session.+("site" -> id)
    }
    super.onRouteRequest(request)
  }

}

然后,显然,您必须创建一个数据库模型来根据请求域和/或请求中设置的会话值来查询站点。如果有人知道更好的方法,我很乐意听到。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
相关资源
最近更新 更多