Java Guice@annotations方法类似于Guice MapBinder方法

Java Guice@annotations方法类似于Guice MapBinder方法,java,dependency-injection,annotations,guice,guice-3,Java,Dependency Injection,Annotations,Guice,Guice 3,我是新来的Guice DI。我想澄清我的想法 简单地说,有没有通过Guice@annotations替换MapBinder 我的设想: Interface A{} Class A1 implements A{} Class A2 implements A{} 我想注入A的实现类,如下所示 if(param = One) then Inject A1 to A if(param = Two) then Inject A2 to A Class A1 implements A { @I

我是新来的Guice DI。我想澄清我的想法

简单地说,有没有通过Guice@annotations替换MapBinder

我的设想:

Interface A{}
Class A1 implements A{}
Class A2 implements A{}
我想注入A的实现类,如下所示

if(param = One) then Inject A1 to A
if(param = Two) then Inject A2 to A
Class A1 implements A
{     
@Inject(param = One)     
A1(){}    
}

Class A2 implements A
{     
@Inject(param = Two)     
A2(){}    
}
我知道上面的内容可以用MapBinder完成,但我想通过如下注释来完成

if(param = One) then Inject A1 to A
if(param = Two) then Inject A2 to A
Class A1 implements A
{     
@Inject(param = One)     
A1(){}    
}

Class A2 implements A
{     
@Inject(param = Two)     
A2(){}    
}
因此,使用params注释类可以根据参数1或2自动拾取并注入类

既然@Inject不能接受参数,在这种情况下重写@Inject会有帮助吗?如果是,我们如何做

或者这种情况只能通过使用MapBinder绑定来实现。我不想使用binder的原因是我们想显式地定义键值对的绑定映射,但使用注释只是简单地用参数注释实现类-更易于维护

提前感谢。

来自JLS,§9.6

由于AnnotationTypeDeclaration语法,注释类型声明不能是泛型的,并且不允许使用extends子句

注释类型不能显式声明超类或超接口的结果是,注释类型的子类或子接口本身永远不是注释类型。类似地,java.lang.annotation.annotation本身不是一种注释类型

因此,不,重写[sic]不会有帮助,因为任何扩展类型都不能是注释类型。

来自JLS,§9.6

由于AnnotationTypeDeclaration语法,注释类型声明不能是泛型的,并且不允许使用extends子句

注释类型不能显式声明超类或超接口的结果是,注释类型的子类或子接口本身永远不是注释类型。类似地,java.lang.annotation.annotation本身不是一种注释类型


因此,不,重写[sic]不会有帮助,因为没有扩展类型可以是注释类型。

为了回答您的后续问题,我相信您正在寻找的是命名注入。请参见此示例:

public class GuiceNamedTest extends AbstractModule {

    public static void main(String[] args) {
        Injector i = Guice.createInjector(new GuiceNamedTest());
        i.getInstance(InstaceOne.class);
        i.getInstance(InstaceTwo.class);
    }

    @Override
    protected void configure() {
        Bean beanOne = new Bean();
        beanOne.name = "beanOne";

        Bean beanTwo = new Bean();
        beanTwo.name = "beanTwo";

        bind(Bean.class).annotatedWith(Names.named("one")).toInstance(beanOne);
        bind(Bean.class).annotatedWith(Names.named("two")).toInstance(beanTwo);

        bind(InstaceOne.class);
        bind(InstaceTwo.class);
    }


    public static class Bean {
        String name;
    }

    public static interface A {}

    public static class InstaceOne implements A {

        @javax.inject.Inject
        public InstaceOne(@Named("one") Bean b1) {
            System.out.println(b1.name);
        }
    }

    public static class InstaceTwo implements A {

        @javax.inject.Inject
        public InstaceTwo(@Named("two") Bean b1) {
            System.out.println(b1.name);
        }
    }

}
在这里,我使用annotatedWith来命名我的guice处理实例。其中一个对应于字符串1,另一个对应于字符串2,类似于您的示例

然后,在A的实现中,我可以使用@Named注释对这些bean进行特定的注入

运行上述代码时的结果是:

beanOne
beanTwo
如您所见,它将mybean的正确实例注入到正确的实现中

希望有帮助


Artur

为了回答您的后续问题,我相信您正在寻找的是名为注射剂的注射剂。请参见此示例:

public class GuiceNamedTest extends AbstractModule {

    public static void main(String[] args) {
        Injector i = Guice.createInjector(new GuiceNamedTest());
        i.getInstance(InstaceOne.class);
        i.getInstance(InstaceTwo.class);
    }

    @Override
    protected void configure() {
        Bean beanOne = new Bean();
        beanOne.name = "beanOne";

        Bean beanTwo = new Bean();
        beanTwo.name = "beanTwo";

        bind(Bean.class).annotatedWith(Names.named("one")).toInstance(beanOne);
        bind(Bean.class).annotatedWith(Names.named("two")).toInstance(beanTwo);

        bind(InstaceOne.class);
        bind(InstaceTwo.class);
    }


    public static class Bean {
        String name;
    }

    public static interface A {}

    public static class InstaceOne implements A {

        @javax.inject.Inject
        public InstaceOne(@Named("one") Bean b1) {
            System.out.println(b1.name);
        }
    }

    public static class InstaceTwo implements A {

        @javax.inject.Inject
        public InstaceTwo(@Named("two") Bean b1) {
            System.out.println(b1.name);
        }
    }

}
在这里,我使用annotatedWith来命名我的guice处理实例。其中一个对应于字符串1,另一个对应于字符串2,类似于您的示例

然后,在A的实现中,我可以使用@Named注释对这些bean进行特定的注入

运行上述代码时的结果是:

beanOne
beanTwo
如您所见,它将mybean的正确实例注入到正确的实现中

希望有帮助


阿图尔

非常感谢。这很有帮助。那么有没有通过Guice@annotations替换MapBinder?非常感谢。这很有帮助。那么有没有通过Guice@annotations替换MapBinder?嗨,我相信您会为此使用命名绑定。a班在做什么@Inject@Namedone然后将命名的注入类绑定到一个或两个HI—我相信您会为此使用命名绑定。a班在做什么@Inject@Namedone然后将名为的注入类绑定到一个或两个