How to retrieve annotated instance from Guice's injector?

后端 未结 2 466
我寻月下人不归
我寻月下人不归 2021-01-30 02:47

Let\'s say I have a module:

Module extends AbstractModule
{
  @Override
  protected void configure()
  {
    bind(String.class).
      annotatedWith(Names.named(         


        
相关标签:
2条回答
  • 2021-01-30 03:25

    I'm using the following method

    public <T> T getInstance(Class<T> type, Class<? extends Annotation> option) {
        final Key<T> key = Key.get(type, option);
        return injector.getInstance(key);
    }
    

    for this. In general, you still have the problem of creating the annotation instance, but here Names.named("annotation") works.

    0 讨论(0)
  • 2021-01-30 03:37
    injector.getInstance(Key.get(String.class, Names.named("annotation")));
    
    0 讨论(0)
提交回复
热议问题