在 Guice 中注入字符串列表
Injecting list of strings in Guice
我正在尝试在 Guice 中注入一个字符串数组列表。为此,我试图在模块中构建如下列表,稍后我可以将其注入任何 class:
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)
在这种情况下,我发现了这个:,但我认为给定的解决方案不适合我的用例。
使用.toInstance()
myList.addBinding().toInstance("Test1");
myList.addBinding().toInstance("Test2");`
我正在尝试在 Guice 中注入一个字符串数组列表。为此,我试图在模块中构建如下列表,稍后我可以将其注入任何 class:
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)
在这种情况下,我发现了这个:
使用.toInstance()
myList.addBinding().toInstance("Test1");
myList.addBinding().toInstance("Test2");`