Java Guice注入注释值

Java Guice注入注释值,java,dependency-injection,guice,Java,Dependency Injection,Guice,您好,我想向参数注入注释值。比如说 @BindingAnnotation @Target({ ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { int value() default 0; } public class A { @Inject @MyAnnotation(30) protected

您好,我想向参数注入注释值。比如说

@BindingAnnotation
@Target({ ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    int value() default 0;
}

public class A {
    @Inject @MyAnnotation(30)
    protected Integer a;
}
如何在
变量中插入30

非常感谢

用作

您只需在整数字段上注释
@Inject和@MyAnnotation


注意: 如果您的
MyAnnotation
注释还有一个元素,比如
stringValue()
like

@BindingAnnotation
@Target({ ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    int value() default 0;
    String stringValue default "";
}
在以下情况下,为该元素
bindcontent().annotatedWith(MyAnnotation.class).to(“someValue”)
添加一个绑定似乎有效,但我觉得这不是正确的方法

public class A {
    @Inject
    public A(@MyAnnotation Integer integer, @MyAnnotation String string) {
      //Here integer will be 10 and string will be someValue
    }
}
用作

您只需在整数字段上注释
@Inject和@MyAnnotation


注意: 如果您的
MyAnnotation
注释还有一个元素,比如
stringValue()
like

@BindingAnnotation
@Target({ ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    int value() default 0;
    String stringValue default "";
}
在以下情况下,为该元素
bindcontent().annotatedWith(MyAnnotation.class).to(“someValue”)
添加一个绑定似乎有效,但我觉得这不是正确的方法

public class A {
    @Inject
    public A(@MyAnnotation Integer integer, @MyAnnotation String string) {
      //Here integer will be 10 and string will be someValue
    }
}

可能重复用户使用另一个名为
@Inject@MyAnnotation(20)受保护整数b的字段的注释时发生的情况的可能重复@注入@MyAnnotation(2)受保护的整数c@Shay\u t我不明白你的意思。这里的绑定似乎是按类型进行的(正如您从我的第二个代码中看到的,它有一个字符串和一个整数)。您正在绑定一个特定的值“bindcontent().annotatedWith(MyAnnotation.class.)。to(“someValue”)。我想在使用注释时绑定注释值。“@MyAnnotation(stringValue=“您好,我是将分配给str变量的值”)String str;”当用户使用另一个名为
@Inject@MyAnnotation(20)受保护整数b的字段的注释时会发生什么@注入@MyAnnotation(2)受保护的整数c@Shay\u t我不明白你的意思。这里的绑定似乎是按类型进行的(正如您从我的第二个代码中看到的,它有一个字符串和一个整数)。您正在绑定一个特定的值“bindcontent().annotatedWith(MyAnnotation.class.)。to(“someValue”)。我想在使用注释时绑定注释值。“@MyAnnotation(stringValue=“您好,我是将分配给str变量的值”)String str;”