问题描述
我想问一下Play与Java Web的ServletContextListener
有什么等效之处.
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
在Java网络世界中,通常将其链接到ServletContextListener#contextIntialized
In the Java web world, this is usually hooked into ServletContextListener#contextIntialized
我搜索了stackoverflow,那里有一些答案,但是已经过时了
I searched stackoverflow,there were some answers there,but had been out dated
推荐答案
在不完全知道您正在执行的初始化工作"的情况下,很难给出完整的答案.但我建议您从以下位置开始是一个不错的起点:声明Module
如Play DI文档中所述.
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.
如果您编写扩展AbstractModule
的基本Module
:
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() = {
...
}
}
并在您的application.conf
中启用它:
play.modules.enabled += "modules.MyModule"
您有个不错的地方可以对应用程序的Environment
和Configuration
设置进行操作.
You've got a great place to act on your app's Environment
and Configuration
settings.
文档并没有真正涉及到它,但是在这里您还可以调用有趣的方法,例如bindListener()
,当您声明的每个依赖项由Google Guice提供"时,您都可以得到通知.迷上Guice之后,您可以做很多事情-也许从Guice文档开始这里.
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.
这篇关于与Java的ServletContextListener等效的东西是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!