Initializing data with eager binding in play

I just came across a scenario where I had not to choose Play GlobalSettings to execute some tasks on start up of the application, as it is being deprecated in Play 2.5. So as the alternative what we can do to make things happen at the time when application just gets started.

So whatever was done using GlobalSetting.onStart and GlobalSetting.beforeStart needs to be implemented using the dependency injection.

For this, we will just provide the eager binding for the class implementation that needs to perform some tasks just before the application actually gets started.

(Eager binding is used to start up a service when an application starts.)

Then provide those implementations in the constructor of the class for which we have provided eager binding.

So lets start going step by step to replace our onStart/beforeStart implementations with dependency injected class.

Firstly create a class say, MyDBModule which extends AbstractModule in which we will bind the class implementation that will execute the methods at the time of application start up.

class MyDBModule extends AbstractModule {
override def configure() = {

This class MyDBModule is a Guice module that tells Guice how to bind several different types.

Here, the overriding configure method of Abstract module specifies binding of the implementation of class InitialData as an eager  singleton object, which means create it eagerly when the application starts up, that is it invoked at the time of application start up.

Well, this is something new in play 2.5. If you have that class named as Module instead of MyDBModule in root package, the play is going to use it automatically without the need of defining its location in the application.conf.

But if you want to create this class in any other location than root and with some other name than Module then you just need to specify its location in application.conf by adding play.modules.enabled settings.

For example, suppose if you have this guice module in package called bootstrap inside root package. Then just specify its location in application.conf as :

play.modules.enabled = “bootstrap.MyDBModule”

Now the final thing you need to be done is writing the code in the constructor of the dependency injected class that is to be executed at the initialization of the application.

class InitialData @Inject() (employeeRepo: EmployeeRepository) {
//whatever code you want to be executed on start up

There can be a scenario when this code needs some application to be started at some places, like WSClient which needs an implicit parameter ‘Application’ to be passed.

In such situation, we can simply inject the Application explicitly to the class which is bound eagerly.

That’s all you need to be able to do anything that needs to happen on start up!

References :-




This entry was posted in Scala. Bookmark the permalink.

One Response to Initializing data with eager binding in play

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s