Inject: Example to use Constructor in Module without instance.. #259
-
Hi guys.. First, thanks for the great work in this project! I only use inject for now and I like it a lot! So I struggle with something that seems very basic: I want to generate a binding for an Instance that has a Constructor with 1 Dependency - but without using/referencing the instance itself. Just with the Seems simple? The use caseLet's say we have this class that needs injection public class MyClass {
private final MyDepInterface dep;
public MyClass(MyDepInterface dep) {
this.dep = dep;
}
} And what we want to inject is an implementation of public class MyDepImpl implements MyDepInterface {
} Create a binding?Via annotation? - How?First of all, my first question:
ProgrammaticSo ok, I couldn't solve it by annotations, I created a ModuleBuilder builder = ModuleBuilder
.create();
Class<?> clazz = classInfo.load(); // this is a Class for MyClass - the dependency MyDepImpl is bound.
// how to add a binding from 'clazz' to MyClass::new(MyDepImpl.class) - just with the Class?
// i tried this:
Constructor<?> cons = clazz.getConstructor(MyDepImpl.class);
builder.bind(clazz).to(cons, Key.of(MyDepImpl.class));
// which obviously shows:
// Cannot resolve method 'to(Constructor<capture of ?>, Key<T>)' So maybe it's so obvious I cannot see it, but I really tried to go through all the docs and search the issues and discussions - I couldn't find anything(?).. also, there seems to be no documentation of to use all those Thanks so much! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi, @narcoticfresh I do not quite get the part where you need to create the binding without using/referencing the instance itself, but we actually have an example for injecting a class using an interface dependency. The example is not featured on a website, we will add it to the website documentation soon. Take a look at the bind(MessageSender.class).to(ConsoleMessageSenderImpl.class); We instruct the injector that whenever the Another binding is: bind(Application.class).to(Application::new,MessageSender.class); We “teach” the injector that in order to create an instance of This example covers the basic use-case for creating an instance with interface dependencies. If for some reason, you still need to use a reflection to create an instance of your class, you can do so like this, for example: bind(Application.class).to(Application::new,MessageSender.class); with bind(Application.class).to(messageSender -> {
try {
return Application.class
.getConstructor(MessageSender.class)
.newInstance(messageSender);
} catch (InstantiationException |
IllegalAccessException |
InvocationTargetException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
}, MessageSender.class); Just make sure to change the Alternatively, if you can modify a class that you want to instantiate, you could put an bind(Application.class).to(Application::new,MessageSender.class); with bind(Application.class); This binding informs the injector about the existence of the |
Beta Was this translation helpful? Give feedback.
-
Hi @eduard-vasinskyi Let's see those 2 approaches: Construct myselfbind(Application.class).to(messageSender -> {
try {
return Application.class
.getConstructor(MessageSender.class)
.newInstance(messageSender);
} catch (InstantiationException |
IllegalAccessException |
InvocationTargetException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
}, MessageSender.class); That's what I did first.. but obviously, any instance injections will not be processed this way.. That may be not a problem anymore though as I tried to loose those and consolidate into a constructor dependency. I guess this approach could be combined with an instance injector if needed(?).. That is totally doable, but I would prefer a binding based solution. Preferred approachSo as you rightly point out in this snippet: bind(Application.class).to(Application::new, MessageSender.class); I'm basically trying to construct the function reference to I tried to fiddle around to convert a Why would that be needed?The background is as follows: I have OutcomeNow, as you say - yes, I could make the instance myself. But I'm looking for a binding based solution. Do you see any way to construct a "function reference" (as in |
Beta Was this translation helpful? Give feedback.
Hi, @narcoticfresh
I do not quite get the part where you need to create the binding without using/referencing the instance itself, but we actually have an example for injecting a class using an interface dependency. The example is not featured on a website, we will add it to the website documentation soon.
Take a look at the
Application
class. It has a single dependency to theMessageSender
interface. InApplicationModule
we configure the required bindings. First binding is:We instruct the injector that whenever the
MessageSender
interface is required, we would actually use aConsoleMessageSenderImpl
implementation.Another bi…