Skip to main content


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 14:10 Go to next message
Matthieu Wipliez is currently offline Matthieu WipliezFriend
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 #805545 is a reply to message #805224] Thu, 23 February 2012 22:09 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
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 22:57 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
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 Go to previous messageGo to next message
Matthieu Wipliez is currently offline Matthieu WipliezFriend
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 Smile
Re: How to use dependency injection in Eclipse 4? [message #807968 is a reply to message #806110] Mon, 27 February 2012 08:39 Go to previous message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
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: Thu Mar 28 23:45:16 GMT 2024

Powered by FUDForum. Page generated in 0.02329 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top