玩!2.0 Scala - 访问全局对象 [英] Play! 2.0 Scala - Accessing global object
问题描述
我已经声明了一个在应用程序启动时被实例化的对象.我想在控制器中访问它,它是插件的一部分.我希望能够使用该插件,但我似乎无法超越第一部分——找到 MyWebsocketConnection
对象.没有一个示例显示如何执行此操作.我不想注入控制器,因为我正在编写一个插件(我在某处看到了如何执行此操作的静态示例).
I've declared an object which gets instantiated on application start. I want to access it inside a controller, which is part of a plugin. I want to be able to use that plugin, but I can't seem to get past the first part -- finding the MyWebsocketConnection
object. None of the examples show how to do this. I don't want to inject into the controller because I'm writing a plugin (I saw static examples of how to do that somewhere).
Global.scala
,插件应用程序appGlobal.scala
Global.scala
, plugin application appGlobal.scala
object Global extends GlobalSettings {
object MyWebsocketConnection {
val logger = // return something that gets instantiated once, like websocket object for logging to ui
}
class MyWebsocketConnection {
import MyWebsocketConnection.logger
}
override def onStart(app: Application) {
Logger.info("Application has started...");
}
}
我的自定义日志插件控制器:
My custom logging plugin controller:
MyLogger.Scala
,插件应用程序appcontrollersMyLogger.scala
MyLogger.Scala
, plugin application appcontrollersMyLogger.scala
object MyLogger {
def info(message: String) = {
// THIS CAN'T BE FOUND ?
// MyWebsocketConnection.logger.send(message)
}
}
所以,从戏剧!2.0 应用程序引用该插件,我会(可能)做类似下面的事情,但我什至无法超越这之前的部分:
So, from the Play! 2.0 app that references the plugin, I would (probably) do something like below, but I can't even get past the part before this:
MyFutureController.scala
,又一个 Play!应用程序appcontrollersMyFutureController.scala
MyFutureController.scala
, another Play! application appcontrollersMyFutureController.scala
object MyFutureController extends Controller {
def someRandomMethod = Action {
// Custom logging
MyLogger.info("Here's my log message!");
Ok("This documentation stinks!")
}
}
推荐答案
还有解决方法 #3:将您的 Global 类移动到一个包中,并在 application.conf 文件中指定其完全限定名,如下所示:
There is also workaround #3: move your Global class to a package and specify its fully qualified name in application.conf file, like so:
global=my.packaged.Global
global= my.packaged.Global
这篇关于玩!2.0 Scala - 访问全局对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!