有没有办法将假装目标绑定到guice? 我的用例如下:
答案 0 :(得分:0)
我使用Google Guice中的Provider
实现解决了这个问题。
这是一个样本
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());
}
}