For instance, RESTEasy's ResteasyWebTarget class has a method proxy(Class<T> clazz)
, just like Injector's getInstance(Class<T> clazz)
. Is there a way to tell Guice that creation of some classes should be delegated to some instance?
My goal is the following behavior of Guice: when the injector is asked for a new instance of class A, try to instantiate it; if instantiation is impossible, ask another object (e. g. ResteasyWebTarget instance) to instantiate the class.
I'd like to write a module like this:
@Override
protected void configure() {
String apiUrl = "https://api.example.com";
Client client = new ResteasyClientBuilder().build();
target = (ResteasyWebTarget) client.target(apiUrl);
onFailureToInstantiateClass(Matchers.annotatedWith(@Path.class)).delegateTo(target);
}
instead of
@Override
protected void configure() {
String apiUrl = "https://api.example.com";
Client client = new ResteasyClientBuilder().build();
target = (ResteasyWebTarget) client.target(apiUrl);
bind(Service1.class).toProvider(() -> target.proxy(Service1.class);
bind(Service2.class).toProvider(() -> target.proxy(Service2.class);
bind(Service3.class).toProvider(() -> target.proxy(Service3.class);
}
I've thought about implementing Injector interface and use that implementation as a child injector, but the interface has too much methods.
I can write a method enumerating all annotated interfaces in some package and telling Guice to use provider for them, but that's the backup approach.