in earlier versions read with a bang

val value = Play.current.configuration.getString("confKey") 

but play 2.5 issues a warning

This is a static reference to an application, use DI instead.

which means - guys use Dependency Injection

Well, I write the following code:

 import javax.inject.Inject import play.api.Configuration class SomeClass @Inject() (playconfiguration: Configuration) { val someConfigValue: String = playconfiguration.getString("someConfKey").get } 

Need access to config из объекта . Ok, we inherit the class and give it the configuration, then I have a problem, I do not understand where to get it

 object SomeObj extends SomeClass(Configuration.и что дальше?`){ def getSomeConfigValue(): String = someConfigValue } 

reading from both application.conf and somePath/someFile.conf

In the end, the question can be reformulated - " how to read the key value from application.conf or * .conf in an object, bypassing the warning about DI, that is, not using Play.current.configuration but using Dependency Injection ? "

 object AnyObj { val value = читаем из конфигурационного файла } 

Help me to understand.

    2 answers 2

    As far as I know such inheritance is impossible. If it is necessary for object to have access to dependencies, I see two options:

    1. Pass dependency as a function parameter. From the class in which you need this function.

       object SomeObj { def getSomeConfigValue(configuration: Configuration): String = { configuration .getString("play.crypto.secret") .getOrElse(sys.error(s"Please configure secret") } } //example class MyController @Inject()(configuration: Configuration) extends Controller { def index = Action { val secret = SomeObj.getSomeConfigValue(configuration) println(secret) Ok } } 
    2. To create an object inside a class (which is better to make a singleton). (This method is an extreme case)

       @Singleton class InjectableClazz @Inject() (configuration: Configuration) { object SomeObj { def getSomeConfigValue(): String = someConfigValue } } 
    • and how, for example, in the 1st case I read the key value from application.conf? How to pass it to the constructor? - Vyacheslav Danshin
    • @papiroca, added an example. If this is not your case, it is better to give an example closer to life, otherwise the question is too abstract. To extract a dependent value from an object — usually this is the result of a wrong design — and here we have to act on the situation. And you did not describe the situation. - EnverOsmanov
    • in the line val secret = SomeObj.getSomeConfigValue (configuration) - which configuration will be used, application.conf? and if I want pathToConf / myConf.conf? - Vyacheslav Danshin
    • about the design, for example, the task, create an AppConf class that reads the configuration I specified through Inject , then I would like to get access to any value from the configuration but not to do new, and not to do Inject, but to take the necessary value from a ConfReader object that the AppConf class will inherit, for example val db = MongoClient(ConfReader.getDbHost, ConfReader.getDbPort) - Vyacheslav Danshin

    I managed to implement it as follows, for example, read the settings for connecting to the database, create the services/ConfigReader.scala file

     package services import javax.inject.Inject import play.api.{Configuration, Environment} class AppConfig @Inject()(playConfig: Configuration) { val dbHost: Option[String] = playConfig.getString("mydb.host") val dbPort: Option[Long] = playConfig.getLong("mydb.port") } object ConfigReader { val config = new AppConfig(Configuration.load(Environment.simple())) def getDbHost: String = config.dbHost.getOrElse("localhost") def getDbPort: Long = config.dbPort.getOrElse(27017) } 

    let's write in conf/application.conf

     mydb { host = 192.168.0.0 port = 1234 } 

    Thus, we will be able to read the config from anywhere, simply by importing a ConfigReader , for example, in the controller

     package controllers import play.api.mvc._ import services.ConfigReader class SomeCtrl extends Controller { def index = Action { request => Ok(ConfigReader.getDbHost + ":" + ConfigReader.getDbPort.toString) } } 

    Works application.conf with application.conf , it remains only to understand how to read from *.conf