I started using google guice and trying to create an singleton object. Not sure how to pass the parameter required by getClient(...) and create an object using @Inject annotation. Any help is appreciated.
public class Client
{
private readonly Child _child;
public Client(Child child)
{
this._child = child;
}
static class NestedClass {
public String propX;
public String propY;
}
}
class MyHelperModule extends AbstractModule {
private static final String propXValue = "hello";
@Provides
@Singleton
private Client getClient(
String propY) {
Client.NestedClass nestedClass = new Client.NestedClass();
nestedClass.propX("hello");
nestedClass.propY(propY);
return new Client(nestedClass);
}
}
// helper class to create singleton object Client.
// Need to pass param propY while creating the instance
public Client _client;
@Inject
void newClient(Client client) {
this._client = client;
}
Thanks Srini
The arguments to a @Provides
methods are obtained through bindings in Guice. Typically you shouldn't bind something as common as a String
without a binding annotation. So something like this should let the Client
be created:
@BindingAnnotation
@Retention(RUNTIME)
@interface PropX {}
// ...
bind(String.class).annotatedWith(PropX.class).toInstance("propX value");