I want to run some code on startup to pre-cache some stuff, and would also like to start a Timer to re-cache some things outside the critical path of a web request. Is this possible with Play Framework, and where so I put this code?
标签:
playframework
相关问题
- PlayFramework: how to transform each element of a
- Could not import the newly generated play framewor
- Play Framework Unicode symbols in HTTP Header
- Putting output of 'git describe' in templa
- org.scala-sbt#sbt;${{sbt.version}}: not found on e
相关文章
- Testing request with CSRF Token in Play framework
- How to map an abstract collection with jpa?
- play2 framework my template is not seen. : package
- How does @Inject in Scala work
- play framework 2.0 - unexpected exception - Key No
- Error integration fingerprint U.are.U SDK with jav
- Custom bridge table in playframework ebean
- how to handle fileupload into a table in play fram
In Play 2.4.+ you should use dependency injection with an (optional) lifecycle hook. Play will automatically use any class called
Module
that is in the root package.For example:
app/ApplicationStart.scala:
app/Module.scala:
More documentation of this pattern in Playframework docs.
Here's an example in an application.
I need prepare some system properties for logger, but it seems logger is init before eager singletons. So I need do it in a customized application loader:
add to
application.conf
:play.application.loader = MyAppStartup
reference:https://www.playframework.com/documentation/2.6.x/ScalaCompileTimeDependencyInjection#application-entry-point
https://www.playframework.com/documentation/2.6.x/SettingsLogger#using-a-custom-application-loader
Do not forget that the code shown in the above answers has to be in the base package of your app (no package specification). (I am running Play Framework 2.3.2).
Also, the code will not be run in dev mode until the first HTTP request is made!
In my case my code is this:
Located in the project structure as this:
(I am using IntelliJ IDEA).
In playframework 2.0 and above, use GlobalSettings, as the following code:
more information, go to playframework docs: JavaGlobal
You need to create a bootstrap job which will be executed by Play at application start time.
Read more about how to do this in the Play Manual.