Guice在运行时按属性(外部文本文件中的字符串)/进行绑定更改 [英] Guice change binding by property (string in external text file)/ on runtime

查看:57
本文介绍了Guice在运行时按属性(外部文本文件中的字符串)/进行绑定更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过属性/文本文件中给出的属性/字符串更改/设置绑定?就我而言,我想实现一种演示模式". 在正常模式下,属性为外部服务提供url,但如果url为"demo",则应按如下所示更改相应接口的绑定:

how to change/set a binding by a property/string given in a property/text file? in my case i want to implement a kind of "demo mode". In normal mode a property gives an url to an external service but if url is "demo" the binding of the according interface should be changed like this:

正常:

bind(SasDatenProvider.class).to(SasDataProviderHttpImpl.class);

演示

bind(SasDataProvider.class).to(SasDataProviderFileImpl.class);

如何实现? 提前谢谢

推荐答案

您可以在模块中使用带有@Provides注释的方法.然后您可以执行以下操作:

You can use a method annotated with @Provides in your module. Then you can do something like this:

public class MyModule extends AbstractModule {

    @Provides
    SasDatenProvider provideSas(SasDataProviderHttpImpl http, 
        SasDataProviderFileImpl file){

        boolean isDemo = false; /* do you property lookup logic here */

        return isDemo ? file : http;
    }
}

您可以在guice文档中阅读有关@Provides-方法的更多信息: http://code.google.com/p/google-guice/wiki/ProvidesMethods

You can read more about @Provides-methods in the guice docs: http://code.google.com/p/google-guice/wiki/ProvidesMethods

如果您不想在模块中使用这种逻辑,则可以考虑创建自己的提供程序: http://code.google.com/p/google-guice/wiki/ProviderBindings

If your don't want this kind of logic in you module, you can consider creating your own provider: http://code.google.com/p/google-guice/wiki/ProviderBindings

这篇关于Guice在运行时按属性(外部文本文件中的字符串)/进行绑定更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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