How to use dependency injection in Eclipse 4? [message #805224] |
Thu, 23 February 2012 14:10 |
Matthieu Wipliez Messages: 30 Registered: March 2010 |
Member |
|
|
Hi,
I'm writing a set of plugins, and since I've been using Eclipse 4 for a few months, I though it would be time to switch to DI rather than stay with a hard-coded factory or singleton. However, I can't seem to get it to work :-/
In my case, I declare a wizard in an extension point, and in my wizard I have declared a field to inject as follows:
@Inject
private Logger logger;
From what I can see, the wizard is created with a Class.newInstance, and not by the injector. Is is possible to specify in the plugin.xml that it should be injected? (in Xtext there is something like MyDslExecutableExtensionFactory:the.qualified.name.of.MyClass)
What would be the correct way to do that?
Also, I have seen several ways of defining bindings. Is there a recommended one? Where should I define my bindings? Is it ok to do so in the activator?
Cheers,
Matthieu
|
|
|
|
Re: How to use dependency injection in Eclipse 4? [message #805570 is a reply to message #805224] |
Thu, 23 February 2012 22:57 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
When using an extension point the process to use e4 di would be similar
to how xtext uses guice. You'd have to use the extension factory approach.
For views there's the e4 bridge which provides DI for views and editors.
Tom
Am 23.02.12 15:10, schrieb Matthieu Wipliez:
> Hi,
>
> I'm writing a set of plugins, and since I've been using Eclipse 4 for a
> few months, I though it would be time to switch to DI rather than stay
> with a hard-coded factory or singleton. However, I can't seem to get it
> to work :-/
> In my case, I declare a wizard in an extension point, and in my wizard I
> have declared a field to inject as follows:
>
>
> @Inject
> private Logger logger;
>
>
> From what I can see, the wizard is created with a Class.newInstance, and
> not by the injector. Is is possible to specify in the plugin.xml that it
> should be injected? (in Xtext there is something like
> MyDslExecutableExtensionFactory:the.qualified.name.of.MyClass)
> What would be the correct way to do that?
>
> Also, I have seen several ways of defining bindings. Is there a
> recommended one? Where should I define my bindings? Is it ok to do so in
> the activator?
>
> Cheers,
> Matthieu
|
|
|
Re: How to use dependency injection in Eclipse 4? [message #806110 is a reply to message #805570] |
Fri, 24 February 2012 15:08 |
Matthieu Wipliez Messages: 30 Registered: March 2010 |
Member |
|
|
Thank you both for your answers!
Lars, I had seen your tutorials (I think they are the first answer on Google ^^), but I was under the impression that it was about creating an entirely new Eclipse 4 Application, whereas what I'm trying to do is create a product let's say "in the Eclipse 3.x way" for the moment, and just use the DI for now.
Perhaps there is something that I don't understand about how the transition from 3.x to 4.x is supposed to happen? If we go back to my wizard example, should it still be declared using the org.eclipse.ui.newWizards extension point? What about "pure UI" like buttons, toolbars?
Anyway, I succeeded in doing what I wanted to do (the page http://www.toedter.com/blog/?p=194 provided some information). If others are trying to do the same thing, here is what I did:
I added a dependency to org.eclipse.e4.core.di (to use the injectable factory and add bindings) and javax.inject (to use annotations in the code). In my bundle I register bindings with
InjectorFactory.getDefault().addBinding(intf).implementedBy(impl);
I also have an injectable extension factory (a la Xtext) that can load classes as follows:
Class<?> clazz = getBundle().loadClass(clazzName);
result = InjectorFactory.getDefault().make(clazz, null);
This works beautifully
|
|
|
|
Powered by
FUDForum. Page generated in 0.02329 seconds