What's the equivalent thing as ServletContextListener for Java

痴心易碎 提交于 2019-12-24 05:49:13

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/45187140/whats-the-equivalent-thing-as-servletcontextlistener-for-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!