A possible way to go is to use a Singeleton Object which is initialized in the Global.scala.
The Global Object has to go in the scala-root of the application or to be configured through application.conf
.
Singleton for shared Data
in app/shared/Shared.scala
(the name is free)
package shared
object Shared {
private var data: Int = 0
def setData(d: Int) : Unit = data = 0
def getData : Int = data
}
In application.conf
you can set the Global to be called on start of the application (you could also just put a file called Global.scala
in app, that would be used by default)
application.global= settings.Global
shared.initial = 42
In app/settings/Global.scala
object Global extends GlobalSettings {
override def onStart(app: Application) {
// Here I use typesafe config to get config data out of application conf
val cfg: Config = ConfigFactory.load()
val initialValue = cfg.getInt(shared.initial)
// set initial value for shared
Shared.setData(initialValue)
}
}
In Play code to get or set the shared data.
import shared.Shared
Shared.getData
Shared.setData( 8 )