【问题标题】:Injecting list of strings in Guice在 Guice 中注入字符串列表
【发布时间】:2018-02-09 02:51:24
【问题描述】:

我正在尝试在 Guice 中注入一个字符串数组列表。为此,我尝试在模块中构建如下列表,稍后我可以将其注入任何类:

Multibinder<String> myList =
            Multibinder.newSetBinder(binder(), String.class);


myList.addBinding().to("Test1");
myList.addBinding().to("Test2");

在上述行中,我收到以下错误:

The method to(Class<? extends String>) in the type LinkedBindingBuilder<String> is not applicable for the arguments (String)

在这种情况下,我发现了这个:Guice : Inject an ArrayList of Strings,但我认为给定的解决方案不适合我的用例。

【问题讨论】:

  • 你的数组列表在哪里?你想把它注入什么?

标签: java guice


【解决方案1】:

使用.toInstance()

myList.addBinding().toInstance("Test1");
myList.addBinding().toInstance("Test2");`

【讨论】:

    猜你喜欢
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    相关资源
    最近更新 更多