Vyacheslav Danshin Vyacheslav Danshin - 1 year ago 162
Scala Question

Can't read conf file in playframework 2.5

In playframework < 2.5 we can use

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

but in 2.5 we have warning

method current in object Play is deprecated: This is a static reference to application, use DI instead

package tmp

object TmpObj {
val confVal = ??? // read key-value from application.conf or *.conf

So, my question is - "How can i read conf from any object in project use

Please help me understand what i must write in
to get value from
or another

import javax.inject.Inject
import play.api.Configuration

class AppConfig @Inject() (configuration: play.api.Configuration) {
val someConfValue = configuration.underlying.getString("someConfValue")

object ConfigReader extends AppConfig(??????) {
def getSomeConfValue() = someConfValue


Answer Source

for this I created the file services/ConfigReader.scala

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)

I prescrib in the conf/application.conf

mydb {
  host =
  port = 1234

and I had the opportunity to access a config from any location by importing my ConfigReader, for example in controller

package controllers

import play.api.mvc._
import services.ConfigReader

class SomeCtrl extends Controller {
  def index = Action { request =>
    Ok(ConfigReader.getDbHost + ":" + ConfigReader.getDbPort.toString)

its work fine, but i still want do this from myconf/myconf.conf and looking for a way

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download