I'm currently migrating my Play Framework project from 2.3 to 2.4. Right now I need to migrate my Global object. Here's a record from their migration guide:
GlobalSettings.beforeStart and GlobalSettings.onStart: Anything that needs to happen on start up should now be happening in the constructor of a dependency injected class. A class will perform its initialization when the dependency injection framework loads it. If you need eager initialization (because you need to execute some code before the application is actually started), define an eager binding.
First, I need to migrate my Global.onStart code. Among other things, it subscribes to various events using akka. Here's how it usually looks like:
import play.api.Play.current
class ApplicationGlobal @Inject()(@Named("event-handler") eventHandlerActor: ActorRef) {
system.eventStream.subscribe(eventHandlerActor, classOf[SomeEvent])
}
Please, pay attention to the import statement. It is required by the subscribe method. And because of this simple fact I can't define an eager binding for this ApplicationGlobal class, as suggested in migration doc, since the application doesn't exist yet. Therefore, I have a couple of questions:
As far as I understand, defining an eager binding actually means defining an analog to the beforeStart method, since the code executes before the application starts. Is that correct?
If it's correct, then what's the analog for the onStart method? According to the doc, this code should now be happening in the constructor of a dependency injected class. But where exactly should I inject this ApplicationGlobal class? Into Controller class? But I have like 10 of them for different pieces of functionality. Should I inject it into all of them?