我的应用中似乎存在循环依赖性问题。我已经测试了所有类并且它们运行良好,但是在运行我的应用程序时我无法实例化它们。
问题的根源在于此类
public class HttpClientProvider implements Provider<HttpClient> {
@Inject
private UserAgent userAgent;
@Inject
private HttpResponseInterceptor interceptor;
private DefaultHttpClient httpClient = new DefaultHttpClient();
@Override
public HttpClient get() {
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent.getUserAgent());
httpClient.addResponseInterceptor(interceptor);
return httpClient;
}
}
此类提供httpClient并添加拦截器。拦截器看起来像这个提供程序用于提供此类的httpclient
public class ServerCommunicator implements ServerCommunicator {
public static final String URL = "http://something.com/";
private HttpClient httpClient;
private HttpContext httpContext;
@Inject
public ServerCommunicator(HttpClient httpClient, HttpContext httpContext) {
this.httpClient = httpClient;
this.httpContext = httpContext;
}
@Override
public HttpResponse post(String URN, String entity) throws IOException {
HttpPost httpPost = new HttpPost(URL + URN);
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Accept", "application/json");
httpPost.setEntity(new StringEntity(entity));
return httpClient.execute(httpPost, httpContext);
}
@Override
public HttpResponse get(String URN) throws IOException {
return httpClient.execute(new HttpGet(URL + URN), httpContext);
}
@Override
public HttpResponse delete(String URN) throws IOException {
return httpClient.execute(new HttpDelete(URL + URN), httpContext);
}
}
问题出现是因为当用户未进行身份验证时,httpResponseInterceptor也使用ServerCommunicator类向服务器发送请求。
如何解决这个问题?