In general, moving these mechanisms away from the GlobalSettings object also means that you no longer register such βcallbacksβ globally, but bind them to a component / class. This gives the advantage that the initialization and shutdown of a particular component can occur directly inside the corresponding class. However, if you have code that you want to run on startup (or shutdown) that is not tied to a specific component (for example, logging, startup checks, etc.), you will have to create new classes for them and bind them in your module.
Note that in the latter case, you usually bind the corresponding classes as strong single ones (to make sure they are created), while in the first case the classes are created as part of the dependency tree.
Launch . Run the code in the constructor of any class that is controlled by the dependency injection container.
bind(classOf[Hello]).to(classOf[EnglishHello]).asEagerSingleton
- Put code in constructor
class EnglishHello extends Hello { println("hello") }
Please note that asEagerSingleton is not required on its own. Since I assume that you are using the manual for the DI provider, you can learn more about it here: https://github.com/google/guice/wiki/Scopes
Shutdown . In any class that needs to run some shutdown code, register a lifecycle callback. 1. Link the class in the module
bind(classOf[Hello]).to(classOf[EnglishHello]).asEagerSingleton
- Register a lifecycle callback (and enter ApplicationLifecycle)
class EnglishHello @Inject() (lifecycle: ApplicationLifecycle) extends Hello { lifecycle.addStopHook { () => Future.successful(connection.stop()) } }
Note that you might want to cover these classes as single classes, because otherwise you end up registering the stop hooks for each instance - depending on what you do with the stop, this might be what you want. More on this here: https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection#Stopping/cleaning-up
Errors : run your own HttpErrorHandler. The basic idea is that you implement a class with a number of methods that will be called by Play! to the corresponding errors. This is described here: https://www.playframework.com/documentation/2.5.x/ScalaErrorHandling