gpt4 book ai didi

java - 带有 Guice 的通用类型提供程序

转载 作者:行者123 更新时间:2023-11-30 09:30:54 25 4
gpt4 key购买 nike

我正在尝试将类型化对象注入(inject)服务类,但得到了

    com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for com.bookpac.utils.appserver.ILookup<com.bookpac.server.common.IWS> was bound.
while locating com.bookpac.utils.appserver.ILookup<com.bookpac.server.common.IWS>
for parameter 0 at com.bookpac.server.LookupService.<init>(LookupService.java:13)
while locating com.bookpac.server.LookupService

要注入(inject)的字段是ILookup<IWS> lookup ,绑定(bind)是 bind(ILookup.class).toProvider(LookupProvider.class)

当使用@Provides 注释时一切正常,有什么想法吗?

最佳答案

当你使用泛型类型时,你需要使用像这样的 TypeLiteral:

bind(new TypeLiteral<ILookup<IWS>>(){}).toProvider(LookupProvider.class);

来自documentation :

Java doesn't yet provide a way to represent generic types, so this class does.

关于java - 带有 Guice 的通用类型提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com