加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何在Play中启动代码!框架2.4

发布时间:2020-12-16 09:10:30 所属栏目:安全 来源:网络整理
导读:我试图在应用程序启动时打
我试图在应用程序启动时打印“Hello”来控制台.你可以解释一下如何做吗?

我自己试过了

应用程序/模块/ 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
  }
}

在conf / application.conf中我添加了:

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

当我运行激活器运行时,不会打印“Hello”

解决方法

您需要使用 Global object,并覆盖“onStart”方法:

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.

更新:

正确的方法是使用依赖注入,就像问题中描述的一样. GlobalSettings could be removed later

问题中的代码没有问题.我在本地设置中进行了验证.在开发模式“激活器运行”之后,应用程序在生产模式“激活启动”开始之后,代码写入“Hello”.

Btw,尝试使用一些更容易在日志中找到字符串,如

“——– APP DZIABLO已经开始——–”

可能是这样,你刚刚错过了“你好”的日志(我从一开始就不认识它)

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读