Tom
Tom

Reputation: 6342

What's the equivalent thing as ServletContextListener for Java

I would ask what's the equivalent thing for Play as ServletContextListener for Java web.

During the application starts up, I would fetch data from DB and save in memory/cache, and other things that is similar and suitable to be done during server startups

In the Java web world, this is usually hooked into ServletContextListener#contextIntialized

I searched stackoverflow,there were some answers there,but had been out dated

Upvotes: 1

Views: 92

Answers (1)

millhouse
millhouse

Reputation: 10007

It's hard to give a complete answer without knowing exactly what "initialization work" you're doing. But I'd suggest a good place to start would be by declaring a Module as documented in the Play DI documentation.

If you write a basic Module that extends AbstractModule:

package modules

import com.google.inject.AbstractModule
import play.api.{ Configuration, Environment }

class MyModule(
  environment: Environment,
  configuration: Configuration) extends AbstractModule {

  def configure() = {
    ...
  }
}

and enable that in your application.conf:

play.modules.enabled += "modules.MyModule"

You've got a great place to act on your app's Environment and Configuration settings.

The documentation doesn't really go into it, but in here you can also call interesting methods like bindListener() which will allow you to be notified as each of your declared dependencies gets "provisioned" by Google Guice. There's a vast amount of stuff that you can do once you're hooked into Guice - perhaps start at the Guice documentation here.

Upvotes: 1

Related Questions