【问题标题】:How to bind class by using google guice?如何使用 google guice 绑定课程?
【发布时间】:2020-12-09 08:44:04
【问题描述】:

我想使用第三方客户端 API。我想创建一个 ServiceClient 实例并使用 postMessage API。我创建了两个类,ServiceClient 和 ServiceClientAPI 我应该如何绑定呢?非常感谢!


public class ServiceClient {

    @Provides
    @Singleton
    public ServiceClient provideServiceClient() {
        return new ServiceClientBuilder()
                .withEndpoint()
                .withClient(ClientBuilder.newClient())
                .withSystem(SystemBuilder.standard().build())
                .build();
    }



public class ServiceClientAPI {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceClientAPI.class);

    @Inject
    private ServiceClient ServiceClient;

    public Value postMessage(@NonNull Value value) {

        LOGGER.info("Value is " + value);

        try {
            Response response = ServiceClient.postMessage(value);
            return response;
        } catch (Exception ex) {
            String errMsg = String.format("Error hitting the Service");
            LOGGER.error(errMsg, ex);
            throw new Exception(errMsg, ex);
        }
    }

It doesn't work, how should I bind them?

public class ServiceModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(ServiceClient.class).to(ServiceClientAPI.class);
    }
}

【问题讨论】:

    标签: java dependency-injection guice


    【解决方案1】:

    看起来你在这里混合了几个概念。

    如果您有一个简单的项目。 我建议将客户端构建器移动到模块并删除 ServiceClient 类。 看起来您的 ServiceClientAPI 是一个包装器,因此不要将 ServiceClient 绑定到 ServiceClientAPI。 @Inject 会为您解决这个问题,只需按原样绑定即可。

    public class ServiceModule extends AbstractModule {
    
        @Override
        protected void configure() {
            bind(ServiceClientAPI.class);
        }
    
        @Provides
        @Singleton
        public ServiceClient provideServiceClient() {
            return new ServiceClientBuilder()
                    .withEndpointFromAppConfig()
                    .withClient(ClientBuilder.newClient())
                    .withSystem(SystemBuilder.standard().build())
                    .build();
        }
      
    }
    

    当涉及到较大的项目并且提供具有一些逻辑 inti 时,您可能希望在它们自己的类中使用提供者。 在这种情况下,创建一个 ServiceClientProvider

    public class ServiceClientProvider implements Provider<ServiceClient> {
    
    
        @Override
        public ServiceClient get() {
           return new ServiceClientBuilder()
                    .withEndpointFromAppConfig()
                    .withClient(ClientBuilder.newClient())
                    .withSystem(SystemBuilder.standard().build())
                    .build();
        }
    }
    

    模块看起来像

    public class ServiceModuleWithProvider extends AbstractModule {
        @Override
        protected void configure() {
                bind(ServiceClientAPI.class);
        }
    
        @Override
        protected void configure() {
            bind(ServiceClient.class)
                    .toProvider(ServiceClientProvider.class)
                    .in(Singleton.class);
        }
    }
    

    https://github.com/google/guice/wiki/ProviderBindings 还有Guice @Provides Methods vs Provider Classes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      相关资源
      最近更新 更多