在Guice中注入字符串列表 [英] Injecting list of strings in Guice

查看:0
本文介绍了在Guice中注入字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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,但我认为给定的解决方案不适合我的用例。

推荐答案

使用.toInstance()

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

这篇关于在Guice中注入字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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