java - play 2.5 生产模式无法在应用程序启动时运行的 Singleton 类中使用注入(inject)的 ConfigurationProvider 类对象

标签 java dependency-injection guice playframework-2.5

我有一个单例类,我在构造函数中注入(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/

相关文章:

java - 使用 Hibernate 和 Guice 管理 JavaSE 中的事务

Java,程序最后只做了repaint()

c# - 为什么此DI代码声明一个方法范围的var,然后将其分配给一个静态的类级对象?

asp.net - 在使用 MVP 模式构建的 ASP.NET Web 表单应用程序上应用依赖注入(inject)

c# - 从 MVC 中的 Controller 构造函数中删除依赖项

java - 静态类/接口(interface)的 Guice 泛型配置错误

java - 将列表的元素复制到二维数组

java - UcanaccessDriver 抛出 ExceptionInInitializerError 由 : java. lang.RuntimeException : org. hsqldb.jdbc.JDBCDriver 引起

java - 如何实例化 MyList 对象

java - Google Guice+策略模式+反射(reflection)