'无法初始化全局对象'向 Global 类添加最终静态常量后出错 [英] 'Cannot init the Global object' error after adding a final static constant to Global class

查看:60
本文介绍了'无法初始化全局对象'向 Global 类添加最终静态常量后出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是带有 Java 的 Play Framework 2.2.3.

I'm using Play Framework 2.2.3 with Java.

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

I just add a constant to my Global class:

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 中时,我才会收到这个错误.有什么帮助吗?

Before my app was working fine. If I put this constant in any other class, my app is working fine. Only if I put it in Global I get this error. Any help?

推荐答案

this 问题指出,如果你想在应用程序启动时访问配置,你应该使用 onStart 方法.

As in this issue pointed out, if you want to access the configuration during application startup, you should use the onStart method.

这篇关于'无法初始化全局对象'向 Global 类添加最终静态常量后出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆