0

我的应用程序中似乎存在循环依赖问题。我已经测试了所有类并且它们运行良好,但是在运行我的应用程序时我无法实例化它们。

问题的根源在这个类

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 类在用户未经过身份验证时向服务器发送请求。

如何解决这个问题?

4

0 回答 0