向Global类添加最终静态常量后,“无法初始化全局对象”错误

时间:2014-12-07 12:02:55

标签: playframework playframework-2.2

我正在使用Play Framework 2.2.3和Java。

我只是在Global类中添加一个常量:

public static final String VERSION = play.application().configuration().getString("application.version");

然后我收到此错误:

play.api.PlayException: Cannot init the Global object[null]
    at play.api.WithDefaultGlobal$$anonfun$play$api$WithDefaultGlobal$$globalInstance$1.apply(Application.scala:55) ~[play_2.10.jar:2.2.3]
    at play.api.WithDefaultGlobal$$anonfun$play$api$WithDefaultGlobal$$globalInstance$1.apply(Application.scala:49) ~[play_2.10.jar:2.2.3]
    at play.utils.Threads$.withContextClassLoader(Threads.scala:18) ~[play_2.10.jar:2.2.3]
    at play.api.WithDefaultGlobal$class.play$api$WithDefaultGlobal$$globalInstance(Application.scala:48) ~[play_2.10.jar:2.2.3]
    at play.api.DefaultApplication.play$api$WithDefaultGlobal$$globalInstance$lzycompute(Application.scala:399) ~[play_2.10.jar:2.2.3]
    at play.api.DefaultApplication.play$api$WithDefaultGlobal$$globalInstance(Application.scala:399) ~[play_2.10.jar:2.2.3]
Caused by: java.lang.ExceptionInInitializerError: null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_65]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_65]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_65]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_65]
    at java.lang.Class.newInstance(Class.java:379) ~[na:1.7.0_65]
    at play.api.WithDefaultGlobal$class.play$api$WithDefaultGlobal$$javaGlobal(Application.scala:28) ~[play_2.10.jar:2.2.3]
Caused by: java.lang.RuntimeException: There is no started application
    at scala.sys.package$.error(package.scala:27) ~[scala-library.jar:na]
    at play.api.Play$$anonfun$current$1.apply(Play.scala:68) ~[play_2.10.jar:2.2.3]
    at play.api.Play$$anonfun$current$1.apply(Play.scala:68) ~[play_2.10.jar:2.2.3]
    at scala.Option.getOrElse(Option.scala:120) ~[scala-library.jar:na]
    at play.api.Play$.current(Play.scala:68) ~[play_2.10.jar:2.2.3]
    at play.api.Play.current(Play.scala) ~[play_2.10.jar:2.2.3]

在我的应用程序正常工作之前。如果我把它放在任何其他类中,我的应用程序运行正常。只有我把它放在Global中我才会收到此错误。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

正如在this问题指出的那样,如果要在应用程序启动期间访问配置,则应使用onStart方法。