reactnative
reactnative

Reputation: 575

How to bind class by using google guice?

I want to use third party client API. I want to create an instance of ServiceClient and use postMessage API. I created two classes, ServiceClient and ServiceClientAPI How should I bind it? Thanks a lot!


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);
    }
}

Upvotes: 0

Views: 374

Answers (1)

Haim Raman
Haim Raman

Reputation: 12023

It looks like you are mixing few concepts here.

If you have a simple project. I would recommend moving the client builder to the Module and removing the ServiceClient class. It looks like your ServiceClientAPI is a wrapper so don't bind the ServiceClient to the ServiceClientAPI. The @Inject will take care of that for you just bind it as is.

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();
    }
  
}

When it comes to larger project and the provide has some logic inti you may want to use providers in their own classes. In this case create a ServiceClientProvider

public class ServiceClientProvider implements Provider<ServiceClient> {


    @Override
    public ServiceClient get() {
       return new ServiceClientBuilder()
                .withEndpointFromAppConfig()
                .withClient(ClientBuilder.newClient())
                .withSystem(SystemBuilder.standard().build())
                .build();
    }
}

Module will look like

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);
    }
}

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

Upvotes: 1

Related Questions