Guice:在Module(JndiIntegration)中使用参数 [英] Guice: Using parameter in Module (JndiIntegration)

查看:96
本文介绍了Guice:在Module(JndiIntegration)中使用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的模块

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"值?

Well, I think my question is easy, but... how can I parametrize the "URI TO TRANSACTION MANAGER" value??

我的意思是,像这样

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的配置文件,其中包含该文件

and a configuration file called transaction.properties which contains this

  transaction.jndi-uri = URI TO TRANSACTION MANAGER

谢谢!

推荐答案

您应该能够使用单独的提供程序或 @Provides 方法.以下应该可以工作:

You should be able to use a separate provider or a @Provides method. The following should work:

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

这篇关于Guice:在Module(JndiIntegration)中使用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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