次のプロバイダーがあります。
public class GuiceResourceProvider implements Provider<WebResource> {
@Inject
private Configuration configuration;
@Inject
private Environment environment;
private String name;
@Inject
public GuiceResourceProvider(@Assisted("name") String name) {
this.name = name;
}
@Override
public WebResource get() {
if (name == "default") {
String connectionsDSResourceUrl = "http://localhost:" + this.configuration.getHttpConfiguration().getPort();
Client connectionsDSHttpClient = new JerseyClientBuilder().using(this.configuration.getHttpClientConfiguration()).using(this.environment).build();
connectionsDSHttpClient.addFilter(new RequestIdClientFilter());
return connectionsDSHttpClient.resource(connectionsDSResourceUrl);
} else if(name == "other"){
return ......
}
}
}
このプロバイダーの 2 つのインスタンスを作成する必要があります。1 つは "default" という注釈が付けられた WebResource インジェクションをバインドし、もう 1 つは "other" という注釈が付けられた WebResource インジェクションをバインドします。
次のように、Guice に Provider インスタンスを作成させます。
bind(WebResource.class).annotatedWith(Names.named("default")).toProvider(GuiceResourceProvider.class).in(Scopes.SINGLETON);
bind(WebResource.class).annotatedWith(Names.named("other")).toProvider(GuiceResourceProvider.class).in(Scopes.SINGLETON);
注入されたパラメーターは問題ありませんが、カスタム パラメーターを提供することはできません。インスタンスを手動でインスタンス化してカスタム パラメータを指定しようとすると、もちろん注入されたものに問題があります。Assisted 表記に従う場合、問題は、プロバイダーのファクトリーをモジュールに注入する必要があることです。もちろん、これは完全にオフです!
誰でも助けることができますか?