简体   繁体   English

如何在Scala Play 2.5.8中的对象中使用配置

[英]How to use configuration in scala play 2.5.8 in an object

I am migrating from play 2.4.x to play 2.5.8 我正在从游戏2.4.x迁移到游戏2.5.8

While migrating I am getting a lot of deprecation related warning that I am trying to resolve. 迁移时,我收到许多与折旧有关的警告,我试图解决该警告。 Once such error is method current in object Play is deprecated: This is a static reference to application, use DI instead 一旦此类错误成为对象Play中当前方法的不推荐使用:这是对应用程序的静态引用,请改用DI

below is the object 下面是对象

object Foo{
  def testURL= {
   val test =  Play.current.configuration.getString("test.url")
}

If I try to use config = Configuration.load() I get the error overloaded method value load with alternatives: 如果我尝试使用config = Configuration.load()则会收到错误重载的方法值错误以及其他替代方法:

(environment: play.api.Environment)play.api.Configuration (environment: play.api.Environment,devSettings: Map[String,AnyRef])play.api.Configuration cannot be applied to () (环境:play.api.Environment)play.api.Configuration(环境:play.api.Environment,devSettings:Map [String,AnyRef])play.api.Configuration不能应用于()

I there a way to use the play.api.configuration here ? 我在这里可以使用play.api.configuration吗? I don't want to convert object into singleton class. 我不想将对象转换为单例类。

I'm basing this answer on this group post because it's about as good an answer as you'll get for this topic. 我将这个答案作为本组帖子的基础,因为它与您为该主题获得的答案一样好。

What you're trying to do is an anti-pattern, because something is an object it should not depend on external state. 您正在尝试做的是反模式,因为某些东西是对象,它不应该依赖于外部状态。 Technically, configuration is based on the state of a file on the filesystem, so it is state in that sense. 从技术上讲,配置基于文件系统上文件的状态,因此从某种意义上来说就是状态。

To do this cleanly, you should use a class. 为了干净地做到这一点,您应该使用一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM