HK2 IterableProvider命名方法未找到实现 [英] HK2 IterableProvider named method not finding Implementation

查看:334
本文介绍了HK2 IterableProvider命名方法未找到实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,试图注入一个与它绑定的两个服务的合同。



我正在使用泽西,并扩展 ResourceConfig 来配置我的应用程序,在那里我绑定了两个不同的实现(class FooImpl1 FooImpl2 )到同一个合同(界面 Foo ),排名不同。每个这些实现都用<$​​ c $ c> @Named 及其名称进行注释。



在我的一个控制器中,我想要访问这两个实现,所以我注入一个 IterableProvider&Foo> fooProvider



如果我没有指定任何东西,最高排名的实现总是注入,这是我想要的。 >

当我想要一个具体的实现,其中之一出现问题。当我调用 fooProvider.named(nameOfTheClass).get()时,将我返回null,但是如果我遍历fooProvider,我可以访问这两个实现,所以他们被注入。



任何人都有一个想法可以丢失什么?



非常感谢您的帮助

解决方案

是的,我不知道为什么它不适用于 @Named 注释值,正如 javadoc ,但不需要任何注释,我们可以在我们进行绑定时配置名称。我们可以通过 命名为 方法。

  register(new AbstractBinder(){
@Override
public void configure(){
bind(Foo1Impl.class).named(foo1)to(Foo.class);
bind(Foo2Impl.class).named(foo2)。(Foo.class);
}
});






更新



所以上面的解决方案已经过测试。如果您仍然遇到问题,请发布一个完整的可运行示例,显示它不工作,如下面(正在工作)



接口和实现

  public interface Greeter {
String getGreeting(String name);
}

public class EnglishGreeter implements Greeter {
@Override
public String getGreeting(String name){
returnHello+ name +! ;
}
}

public class SpanishGreeter implements Greeter {
@Override
public String getGreeting(String name){
returnHola + name +!;
}
}

资源

  @Path(greeting)
public class GreetingResource {

@Inject
private IterableProvider< Greeter>迎宾员;

@GET
public Response getResponse(@QueryParam(lang)String lang,
@QueryParam(name)String name)throws异常{

Greeter greeter = greeters.named(lang).get();
String message = greeter.getGreeting(name);
return Response.ok(message).build();
}
}

绑定。我在功能中执行,但在$ code> ResourceConfig 中,这一切都是一样的。

  @Provider 
public class GreetingFeature实现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);
}
});
返回true;
}
}

结果




I have a problem trying to inject a contract with two services bound to it.

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.

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.

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?

Thanks a lot for your help.

解决方案

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)

Interface and Implementations

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 + "!";
    }
}

Resource

@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();
    }
}

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;
    }  
}

Result

这篇关于HK2 IterableProvider命名方法未找到实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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