Suppose I have a third-party class:
public class ThirdParty {
public ThirdParty(String arg1, String arg2);
}
Since this is third-party, I cannot add my own @Inject annotation, forcing me to use bind-to-constructor. The question is how I can create a ThirdParty with arg1 and arg2 different. In essence, add "Named" annotations from the outside, so to speak.
How about something like:
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);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.