Provider methods can have parameters and those parameters are injected.

@Provides
@Named("HandlerGroup1") Handler[] getHandlerGroup1(Provider<Handler> 
handlerProvider) {
    Handler[] handlers = new Handler[10];

    for (int i = 0; i < handlers.length; i++) {
        handlers[i] = handlerProvider.get();
    }

    return handlers;
}

On Wednesday, 12 February 2014 10:46:31 UTC-5, Denys Zakhozhyy wrote:
>
> While that does work, i found that my problem is on the next level.
>
> So to re-iterate, the problem is below:
>
> package guicetest;
>
> import com.google.inject.*;
> import com.google.inject.name.Named;
> import com.google.inject.name.Names;
>
> import java.util.Arrays;
> import java.util.concurrent.atomic.AtomicInteger;
>
> /**
>  * {@code GuiceTest}
>  */
> public class GuiceTest {
>
>     public static void main(String[] args) {
>
>
>         final Injector injector = Guice.createInjector(new 
> AbstractModule() {
>             @Override
>             protected void configure() {
>                 
> bind(Integer.class).annotatedWith(Names.named("handlerParam")).toInstance(10);
>                 bind(Coordinator.class);
>                 bind(Handler.class).to(HandlerImpl.class);
>             }
>
>
>             @Provides
>             Handler[] handlers() {
>                 final Handler[] handlers = new Handler[10];
>
>                 for (int i = 0; i < handlers.length; i++) {
>
>                     // how should I instantiate handlers here?
>                     handlers[i] = getProvider(Handler.class).get(); // 
> this throws NPE because it this stage binder is set to null
>                 }
>
>                 return handlers;
>             }
>
>         });
>
>
>         final Coordinator instance = 
> injector.getInstance(Coordinator.class);
>
>         System.out.println(instance);
>     }
>
> }
>
>
> interface Handler {
>
> }
>
> class HandlerImpl implements Handler {
>
>     static AtomicInteger instanceCounter = new AtomicInteger();
>
>     private int handlerParam;
>
>     @Inject
>     HandlerImpl(@Named("handlerParam") int handlerParam) {
>         this.handlerParam = handlerParam;
>         instanceCounter.incrementAndGet();
>     }
>
>     @Override
>     public String toString() {
>         return "HandlerImpl{" + "handlerParam=" + handlerParam + '}';
>     }
> }
>
>
> class Coordinator {
>
>     private Handler[] handlers;
>
>     @Inject
>     Coordinator(Handler[] handlers) {
>         this.handlers = handlers;
>     }
>
>     @Override
>     public String toString() {
>         return "Coordinator{" + "handlers=" + Arrays.toString(handlers) + 
> '}';
>     }
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to