假设我有第三方课程:
public class ThirdParty {
public ThirdParty(String arg1, String arg2);
}
由于这是第三方,我无法添加自己的@Inject注释,迫使我使用bind-to-constructor。问题是如何创建一个与arg1和arg2不同的ThirdParty。实质上,可以说从外部添加“命名”注释。
答案 0 :(得分:3)
如下:
class ProvidesExample {
static class ThirdParty {
public ThirdParty(String arg1, String arg2) {}
}
static class Module extends AbstractModule {
@Override
protected void configure() {
bindConstant().annotatedWith(Names.named("arg1")).to("Argument 1");
bindConstant().annotatedWith(Names.named("arg2")).to("Argument 2");
}
@Provides
ThirdParty getThirdParth(@Named("arg1") String arg1, @Named("arg2") String arg2) {
return new ThirdParty(arg1, arg2);
}
}
}