custom Guice binding annotations with parameters

前端 未结 4 530
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 03:41

I have successfully created a Guice binding annotation to inject single threaded java.util.concurrent.ExecutorService instances into a constructor.

here is an exampl

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 04:22

    Look at NamedImpl that implements @Named and method Names.named(). I think you should have same implementation.

    UPDATED Guice compares annotations by hashCode(). So, if you wont use @MultiThreaded(poolSize = 5) you should map it before instancing. It seems like dirty workaround, but you can write smt like

        for (int i = 1; i < 20; i++){
            bind(ExecutorService.class).annotatedWith(Qualifiers.withValue(i)).toProvider(new DependencyProvider(i));
        } 
    

    If you would like to do this. Remember, that your MultiThreadedImpl should override hashCode in proper way. It could be smt like

    @Override
    public int hashCode() {
        return  (127 * "poolSize".hashCode()) ^ value;
    }
    

提交回复
热议问题