HK2 IterableProvider 命名方法未找到实现 [英] HK2 IterableProvider named method not finding Implementation
问题描述
我在尝试注入绑定了两个服务的合同时遇到问题.
I have a problem trying to inject a contract with two services bound to it.
我正在使用 Jersey,并扩展 ResourceConfig
来配置我的应用程序,我在其中绑定了两个不同的实现(类 FooImpl1
和 FooImpl2
>) 到同一个合约(接口 Foo
),对它们进行不同的排名.这些实现中的每一个都使用 @Named
及其名称进行注释.
I'm using Jersey, and extending ResourceConfig
to configure my app, where I'm binding two different implementations (classes FooImpl1
and FooImpl2
) to a same contract (interface Foo
), ranking them differently. Each of these implementations is annotated with @Named
and its name.
在我的一个控制器中,我想访问这两个实现,所以我注入了一个 IterableProvider
.
In one of my controllers I want to have access to both implementations, so I inject an IterableProvider<Foo> fooProvider
.
如果我不指定任何内容,则始终注入排名最高的实现,这正是我想要的.
If I do not specify anything, the implementation with the highest rank is injected always, which is what I want.
当我想要一个具体的实现时就会出现问题,其中之一.当我调用 fooProvider.named( nameOfTheClass ).get()
时,返回 null,但如果我遍历 fooProvider,我可以访问这两个实现,因此它们被注入.
The problem appears when I want a concrete implementation, one of them. When I call fooProvider.named( nameOfTheClass ).get()
, is returning me null, but if I iterate over the fooProvider, I can have access to both implementations, so they are injected.
有人知道我可能遗漏了什么吗?
Anybody has an idea of what could I be missing?
非常感谢您的帮助.
推荐答案
是的,所以我不确定为什么它不适用于 @Named
注释值,因为这就是在javadoc,但是不需要任何注解,我们可以在进行绑定时配置名称.我们可以使用 named
方法.
Yeah so I'm not sure why it doesn't work with the @Named
annotation value, as that's what's stated int the javadoc, but without the need for any annotations, we can configure the name when we do our bindings. We can do so with the named
method.
register(new AbstractBinder(){
@Override
public void configure() {
bind(Foo1Impl.class).named("foo1").to(Foo.class);
bind(Foo2Impl.class).named("foo2").to(Foo.class);
}
});
更新
所以上面的方案已经测试过了.如果您仍然遇到问题,请发布一个完整的可运行示例来演示它不起作用,如下所示(正在运行)
UPDATE
So the above solution has been tested. If you are having problems still, post a complete runnable example that demonstrates it not working, like below (which is working)
接口和实现
public interface Greeter {
String getGreeting(String name);
}
public class EnglishGreeter implements Greeter {
@Override
public String getGreeting(String name) {
return "Hello " + name + "!";
}
}
public class SpanishGreeter implements Greeter {
@Override
public String getGreeting(String name) {
return "Hola " + name + "!";
}
}
资源
@Path("greeting")
public class GreetingResource {
@Inject
private IterableProvider<Greeter> greeters;
@GET
public Response getResponse(@QueryParam("lang") String lang,
@QueryParam("name") String name) throws Exception {
Greeter greeter = greeters.named(lang).get();
String message = greeter.getGreeting(name);
return Response.ok(message).build();
}
}
绑定.我在 Feature
中做到了,但在 ResourceConfig
中,一切都一样.
Binding. I did it in a Feature
, but in a ResourceConfig
, it's all the same.
@Provider
public class GreetingFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
context.register(new AbstractBinder(){
@Override
public void configure() {
bind(EnglishGreeter.class).named("english")
.to(Greeter.class).in(Singleton.class);
bind(SpanishGreeter.class).named("spanish")
.to(Greeter.class).in(Singleton.class);
}
});
return true;
}
}
结果
这篇关于HK2 IterableProvider 命名方法未找到实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!