我正在尝试在 Guice 中注入(inject)一个字符串数组列表。为此,我尝试在模块中构造如下列表,稍后我可以将其注入(inject)到任何类中:
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 ,但我认为给定的解决方案不适合我的用例。
最佳答案
使用.toInstance()
myList.addBinding().toInstance("Test1");
myList.addBinding().toInstance("Test2");`
关于java - 在 Guice 中注入(inject)字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48698033/