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.

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 :

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.

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 :-




Written by 

Rachel Jones is a Solutions Lead at Knoldus Inc. having more than 22 years of experience. Rachel likes to delve deeper into the field of AI(Artificial Intelligence) and deep learning. She loves challenges and motivating people, also loves to read novels by Dan Brown. Rachel has problem solving, management and leadership skills moreover, she is familiar with programming languages such as Java, Scala, C++ & Html.

1 thought on “Initializing data with eager binding in play

Leave a Reply

%d bloggers like this: