繁体   English   中英

Java-依赖注入-第三方库

[英]Java - Dependency Injection - third party libraries

我正在尝试学习依赖注入。 举例来说,我有以下编写的简单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感到有些困惑。

是的,如果遇到此代码,我会将其更改为:

public class UserWebServiceClient
{
  private Client client;

  public UserWebServiceClient(Client client)
  {
    this.client = client;
  }

  ...
}

注入Client允许我传递我选择的Client任何实现,包括模拟实例以测试此类。

此外,在这种情况下,以这种方式更改类还允许使用ClientConfig不同实现。

简而言之,该类变得更加可重用。

最好使用构造函数注入而不是字段注入。 这样做使您可以交换绑定以进行测试。 出于相同的原因,分离凭据也是很好的。

然后可以通过Module或某种形式的配置使您的绑定可用。

使用Guice可能看起来像这样...

    public class UserWebServiceClient
    {
      private Client client;

      @Inject
      public UserWebServiceClient(Client client)
      {            
        this.client = client;
      }

    ...

    }

您的模块

    public class RemoteModule extends AbstractModule {

      public void configure() {
      }

      @Provides
      public Client provideClient(@Named("username") String username, 
            @Named("password") String password) {

        ClientConfig clientConfig = new DefaultApacheHttpClientConfig();
        Client client = ApacheHttpClient.create(clientConfig);

        client.addFilter(new HTTPBasicAuthFilter(
            username,  password));

        return client;
      }

      @Provides
      @Named("username")
      public String provideUsername() {
        return "foo";
      }

      @Provides
      @Named("password")
      public String providePassword() {
        return "bar";
      }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM