Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to use dependency injection in Eclipse 4?
How to use dependency injection in Eclipse 4? [message #805224] Thu, 23 February 2012 09:10 Go to next message
Matthieu Wipliez is currently offline Matthieu Wipliez
Messages: 18
Registered: March 2010
Junior 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 #805545 is a reply to message #805224] Thu, 23 February 2012 17:09 Go to previous messageGo to next message
Lars Vogel is currently offline Lars Vogel
Messages: 1048
Registered: July 2009
Senior Member

See Eclipse 4 Tutorial and Eclipse 4 Services
Re: How to use dependency injection in Eclipse 4? [message #805570 is a reply to message #805224] Thu, 23 February 2012 17:57 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5242
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 10:08 Go to previous messageGo to next message
Matthieu Wipliez is currently offline Matthieu Wipliez
Messages: 18
Registered: March 2010
Junior 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 Smile
Re: How to use dependency injection in Eclipse 4? [message #807968 is a reply to message #806110] Mon, 27 February 2012 03:39 Go to previous message
Lars Vogel is currently offline Lars Vogel
Messages: 1048
Registered: July 2009
Senior Member

Hi Matthieu,

I think a similar description is included in my Eclipse 4 Tutorial, see DI and own Java objects.

Best regards, Lars

Previous Topic:e4 css styling does not work right (Buttons, Text)
Next Topic:Customize perspective doesn't exist anymore ?
Goto Forum:
  


Current Time: Fri Jul 25 09:49:49 EDT 2014

Powered by FUDForum. Page generated in 0.03879 seconds