我有一个单例类,我在构造函数中注入(inject)了 ConfigurationProvider 类。
class Global {
ConfigurationProvider configurationProvider;
@Inject
Global(ConfigurationProvider configurationProvider){
this.configurationProvider = configurationProvider;
DatabaseConfiguration.DB_URL = configurationProvider.get().getConfig("db").getString("default.url");
}
}
当我尝试访问 ConfigurationProvider 类方法时,该对象不可用,并在我以测试生产模式(激活器 testProd)启动时抛出 NullPointerException
我已经在Module类中绑定(bind)了Global类
public class Module extends AbstractModule
{
@Override
public void configure()
{
// Use the system clock as the default implementation of Clock
bind(Clock.class).toInstance(Clock.systemDefaultZone());
// Ask Guice to create an instance of ApplicationTimer when the
// application starts.
bind(ApplicationTimer.class).asEagerSingleton();
// Set AtomicCounter as the implementation for Counter.
bind(Counter.class).to(AtomicCounter.class);
bind(Global.class).in(Singleton.class);
}
}
最佳答案
您需要通过添加来启用您创建的模块
play.modules.enabled += "modules.Module"
在您的application.conf文件中。这样就可以配置依赖项绑定(bind)了。
关于java - play 2.5 生产模式无法在应用程序启动时运行的 Singleton 类中使用注入(inject)的 ConfigurationProvider 类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43464290/