【问题标题】:Guice: Using parameter in Module (JndiIntegration)Guice:在模块中使用参数(JndiIntegration)
【发布时间】:2012-01-25 16:13:01
【问题描述】:

我有一个这样的模块

public class JNDITransactionModule implements Module{

    @Override
    public void configure(Binder binder) {
        binder.bind(TransactionManager.class)
              .toProvider(
                   JndiIntegration.fromJndi(
                      TransactionManager.class, 
                      "URI TO TRANSACTION MANAGER"))
              .in(Scopes.SINGLETON);
    }
}

好吧,我认为我的问题很简单,但是...如何参数化“URI TO TRANSACTION MANAGER”的值??

我的意思是,像这样的

public class JNDITransactionModule implements Module{

    @Override
    public void configure(Binder binder) {
           Properties props = getProperties("transaction.properties");
           Names.bindProperties(binder, props);

        binder.install(new TransactionModule());
        binder.bind(TransactionManager.class)
              .toProvider(
                   JndiIntegration.fromJndi(
                      TransactionManager.class, 
                      "get @Named('transaction.jndi-uri')"))
              .in(Scopes.SINGLETON);
    }
}

还有一个名为 transaction.properties 的配置文件,其中包含此内容

  transaction.jndi-uri = URI TO TRANSACTION MANAGER

谢谢!!!

【问题讨论】:

    标签: jndi guice


    【解决方案1】:

    您应该能够使用单独的提供程序或 @Provides 方法。以下应该有效:

    @Provides
    @Singleton
    TransactionManager provideTransactionManager(@Named("transaction.jndi-uri") String uri) {
        return JndiIntegration.fromJndi(TransactionManager.class, uri).get();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多