HK2 IterableProvider named method not finding Implementation

后端 未结 1 1328
长情又很酷
长情又很酷 2020-12-22 00:51

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

相关标签:
1条回答
  • 2020-12-22 01:55

    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

    enter image description here

    0 讨论(0)
提交回复
热议问题