0
votes

I have the following singleton defined in Scala

package main

import javax.inject._

@Singleton
class Properties {
  val timeout = 120
}

how do I access it from other programs? I tried main.Properties.timeout but it throws a compilation error saying that a companion object was not found

1

1 Answers

1
votes

If you want to access it in the way, that you've mentioned: main.Properties.timeout, then use companion object instead:

class Properties {
   // ...
}
object Properties {
    val timeout = 120
    // ...
}

With @Singleton annotation, you have to inject that service somewhere, to be able to use it. So something like this:

import javax.inject._
import main.Properties

class SomeService @Inject() (props:Properties)() {
    println(props.timeout)
}

Here is documentation about DI for PlayFramework: https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection - for the latest one (not 2.0), but it is a good start point.