14

I im trying to print "Hello" to console on application start. Can You explain how to do it?

What i tried myself:

app/modules/HelloModule.scala:

package modules

import com.google.inject.AbstractModule

trait Hello {}

class MyHelloClass extends Hello {
  initialize() // running initialization in constructor
  def initialize() = {
    println("Hello")
  }
}

class HelloModule extends AbstractModule {
  def configure() = {
    bind(classOf[Hello])
      .to(classOf[MyHelloClass]).asEagerSingleton
  }
}

in conf/application.conf i added:

play.modules.enabled += "modules.HelloModule"

and "Hello" is not printed when i run activator run

dziablo
  • 163
  • 1
  • 7
  • Is "Hello" displayed when the first request is received? In dev mode, play lazily start the app until the first request comes. Btw you can just write `bind(classOf[MyHelloClass]).asEagerSingleton`. – Julien Lafont Sep 15 '15 at 13:30
  • 1
    after `activator run` i request localhost:9000/books (it's simple-rest-scala activator template) and "Hello" is not displayed – dziablo Sep 15 '15 at 15:39
  • See this https://stackoverflow.com/questions/36453955/how-do-i-perform-an-action-on-server-startup-in-the-scala-play-framework – TriCore Aug 28 '17 at 14:17

1 Answers1

7

You need to use Global object, and override "onStart" method:

Defining a Global object in your project allows you to handle global settings for your application. This object must be defined in the default (empty) package and must extend GlobalSettings.

import play.api._

object Global extends GlobalSettings {

  override def onStart(app: Application) {
    Logger.info("Application has started")
  }

  override def onStop(app: Application) {
    Logger.info("Application shutdown...")
  }

}

You can also specify a custom GlobalSettings implementation class name using the application.global configuration key.

Update:

The correct way would be to use Dependency Injection, exactly like it described in the question. GlobalSettings could be removed later

There is no problem with the code in the question. I verified it on my local setup. The code write "Hello" after first request in the development mode "activator run" and after application start in the production mode "activator start".

Btw, try to use some more easy to find string in the log, like

"--------APP DZIABLO HAS BEEN STARTED--------"

It could be so you just missed "Hello" in the log (I did not recognise it from the start)

enter image description here

Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
  • It works, thank You, but should We use GlobalSettings when there is guide how to use dependency injection instead of Global object? [link](https://www.playframework.com/documentation/2.4.x/GlobalSettings) I mean, won't it be deprecated or removed from framework in the future? – dziablo Sep 16 '15 at 10:31
  • for sure you need to use DI, GlobalSettings is just simplest way to "print "Hello" to console on application start" – Andriy Kuba Sep 16 '15 at 11:39
  • @dziablo - look on my update please. I do not see any problems with your code. I did verification on my own local project – Andriy Kuba Sep 16 '15 at 11:53
  • I created fresh scala-play template, added code and it works. Maybe there is something wrong with simple-rest-scala template.. Anyway, thank You very much! – dziablo Sep 16 '15 at 12:21
  • 3
    I don't see how adding a deprecated code snippet is marked up a solution to this problem. The reason that you did not see "Hello" is that activator run starts the server it does not start your application. – Everus Apr 02 '16 at 13:08
  • @Haurus: exactly this explained in the "update" section, is not it? – Andriy Kuba Apr 02 '16 at 15:04