What you are looking for is assisted injection:
http://code.google.com/p/google-guice/wiki/AssistedInject
On 04/10/2014 11:27 AM, Tuan BUI ANH wrote:
Dear all,
Firstly, sorry for my bad English as it's not my native.
I started to learnt to use Guice in my project recently.
My question is: How can I inject a value known by dependent to the
dependency ?
Let's say I have to write a custom web browser using Apache HttpClient.
public class MyHttpClientImpl implements MyHttpClient {
HttpClient client;
ResponseHandler<String> responseHandler;
@Inject
public MyHttpClientImpl(ResponseHandler<String> responseHandler) {}
}
class MyResponseHandler implements ResponseHandler<String>{
void doStuff(){}
@override
public String handleResponse(HttpResponse response){
}
.....
}
It's perfect. However, in handleResponse() I need to access to
MyHttpClientImpl for some action. For example, when I receive a HTTP
302 error code, I would like to redirect my client to the new page.
Without DI, I just need to create a constructor
MyResponseHandler(MyHttpClient myHttpClient) {
this.myHttpClient = myHttpClient;
}
and call it in MyHttpClientImpl:
ResponseHandler<String> responseHandler = new
MyResponseHandler(this);
How can I do it with Guice?
I cannot declare MyHttpClientImpl as @Singleton, as I need lots
instance of this class (ex for multiple tabs)
Currently, I have to manually call
responseHandler.setHttpClient(this) in MyHttpClient, but for sure
it's not good idea.
How can I do ?
Any help are appreciated.
Thanks
Tuan
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.