Play 2.4 discourages using GlobalSettings.onStart and whole Global object.
I'm using play-slick and it has great DI sample in GitHub, but it is missing a example how to do database initialization.
How does one implement the database initialization when using DI and when GlobalSettings.onStart is not available?
Common cases for database initialization is: If in DEV mode, add these rows, if PROD do these. Examples wanted.
The trick is to place the initialisation in the constructor of the injected class. Here's an example:
Add
app/modules/Database.scala:Add in
conf/application.conf:That's to start with. The
.asEagerSingletonwill run the constructor code without needing you to inject it. When you want to choose which one to inject, then you'd need to remove the.asEagerSingletonand load the appropriate database implementation either:in the service/controller, for example:
@Inject @Named("development") Database database