0

is there a way to bind a feign target to guice? My usecase case is as follows:

  1. I have a service, which can be either started in the same JVM or as a separate service.
  2. if the service is started in same JVM, then I will bind it using Guice.
  3. if the service is started outside the jvm, I want to bind the service using fiegn and have guice inject the same.
mihirg
  • 915
  • 2
  • 13
  • 28

1 Answers1

0

I solved this using a Provider implementation in Google Guice. Here is a sample

public class Main {

public static AccountService get() {
    return Feign.builder()
            .contract(new JAXRSContract())
            .decoder(new GsonDecoder())
            .target(AccountService.class, "http://localhost:9090");

}

static class RestClientProvider implements Provider<AccountService> {


    RestClientProvider() {
    }

    @Override
    public AccountService get() {
        return Main.get();
    }

}


static class AppInjector extends AbstractModule {
    @Override
    protected void configure() {
        Provider<AccountService> prov = new RestClientProvider();
        bind(AccountService.class).toProvider(prov);
    }
}

public static void main (String... args) {

    Injector inj = Guice.createInjector(new AppInjector());
    AccountService ac = inj.getInstance(AccountService.class);

    Account a = ac.getAccountByName("Mihir");
    System.out.println(a.getName());

}

}

mihirg
  • 915
  • 2
  • 13
  • 28