【发布时间】:2018-02-06 01:22:01
【问题描述】:
我正在尝试学习依赖注入。例如,我编写了以下简单的 Web 服务客户端,它与 Web 服务对话。
public class UserWebServiceClient
{
private Client client;
public UserWebServiceClient(String username, String password)
{
ClientConfig clientConfig = new DefaultApacheHttpClientConfig();
this.client = ApacheHttpClient.create(clientConfig);
this.client.addFilter(new HTTPBasicAuthFilter(username, password));
}
private WebResource getWebResource()
{
WebResource resource = this.client.resource("http://mywebservice/.......");
return resource;
}
public void createUser(String s) throws StorageAPIException
{
getWebResource().post(...);
}
}
这是依赖注入的候选者吗?我应该注入客户端吗?
我不想把它的复杂性推给用户。我想我对何时使用 DI 有点困惑。
【问题讨论】:
-
程序员 SE 网站上关于 Di 的好帖子:programmers.stackexchange.com/questions/135971/…