【发布时间】:2016-05-27 21:55:37
【问题描述】:
我最近一直在使用 Google Guice,我想出了一个想法,根据声明的类和注释中定义的其他几个参数,将 String 注入到构造函数中。例如:
如果我定义一个新的限定符注解 @NamedInjectable 供 Guice 使用:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Qualifier
public @interface NamedInjectable
{
String name() default "";
boolean indexed() default true;
}
其中 name 是字符串的新名称库(默认仅为类的名称),indexed 说明每次注入新字符串时是否应递增名称。
例如
public MyClass {
@Inject
public MyClass(@NamedInjectable(name = "foo", indexed = true) String name) {
// some code
}
}
并且name 参数应该被赋予一个值,例如“
我考虑过使用 Provider Bindings 或 AssistedInject 但我可以完成它。失败的一个主要原因是不知何故获得了类的名称。
你还有什么想法吗?
【问题讨论】:
标签: java dependency-injection guice