玩! 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
,插件应用程序\ app \ Global.scala
Global.scala
, plugin application \app\Global.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
,插件应用程序\ app \ controllers \ MyLogger.scala
MyLogger.Scala
, plugin application \app\controllers\MyLogger.scala
object MyLogger {
def info(message: String) = {
// THIS CAN'T BE FOUND ?
// MyWebsocketConnection.logger.send(message)
}
}
所以,从Play开始!引用了插件的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
,另一个播放!应用\ app \ controllers \ MyFutureController.scala
MyFutureController.scala
, another Play! application \app\controllers\MyFutureController.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屋!