Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass both injected and assisted parameters to a Guice Provider?

Tags:

guice

I have the following Provider:

  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 ......
        }
    }
}

I need to create 2 instances of this Provider, one that will bind a WebResource injection annotated with "default" and that will bind a WebResource injection annotated with "other".

Letting Guice to create the Provider instance as follows:

    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);

It is ok for the injected parameters but then i cannot provide a custom one. If i try to instantiate an instance manually and provide the custom parameter, then of course the injected ones have the problem. If i follow the Assisted notation, the problem is that i need to inject the Factory of the Provider in the Module and this, of course, is completely off!

Can anyone help?

like image 558
crazypit Avatar asked Dec 03 '25 18:12

crazypit


1 Answers

For everyone that might be interested, i found the answer. If you want to pass both injected AND custom parameters to a Provider, move your injected parameters from the constructor to fields and use the constructor to pass your own parameters. When binding, first create an instance of your Provider passing your parameters to the the constructor, then call requestInjection on this instance in order to add it to the injection graph, and then bind your class to the Provider instance as follows:

GuiceResourceProvider guiceResourceProvider = new GuiceResourceProvider("default");
requestInjection(guiceResourceProvider);

bind(WebResource.class).annotatedWith(Names.named("default")).toProvider(guiceResourceProvider).in(Scopes.SINGLETON);

This is the Provider:

public class GuiceResourceProvider implements Provider<WebResource> {

@Inject
private Configuration configuration;
@Inject
private Environment environment;

private String name;

@Inject
public GuiceResourceProvider(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 ......
    }
}

}

like image 127
crazypit Avatar answered Dec 10 '25 08:12

crazypit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!