Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Dependency Injection in eclipse plugins (not RCP apps)
Dependency Injection in eclipse plugins (not RCP apps) [message #1033388] Thu, 04 April 2013 07:28 Go to next message
Robert Gruendler is currently offline Robert Gruendler
Messages: 66
Registered: June 2011
Member
I'm struggling to understand how to use Dependency Injection features in regular eclipse plugins. All of the tutorials out there only mention how to do this in RCP apps, and i seem to misunderstand something fundamentally here.

Here's an example:

1. I have some service POJO class, annotated with @Creatable
2. I want to inject this service into an org.eclipse.core.runtime.jobs.Job, using the @Inject annotation

When i only use these 2 annotations, no service is being injected, and the service is null at runtime.

However, when i implicitly call "ContextInjectionFactory.inject(this, context);" inside the constructor of the Job class, the service is being injected.

Is this the regular way to do this in eclipse plugins? I would have assumed that the Job subclasses are managed by the eclipse context, hence i don't need to manually inject the dependencies.

Any info on that would be greatly appreciated.

Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1033399 is a reply to message #1033388] Thu, 04 April 2013 07:41 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5419
Registered: July 2009
Senior Member
DI only works if the instance of the object is created using CIF#make

Tom

On 04.04.13 09:28, Robert Gruendler wrote:
> I'm struggling to understand how to use Dependency Injection features in
> regular eclipse plugins. All of the tutorials out there only mention how
> to do this in RCP apps, and i seem to misunderstand something
> fundamentally here.
>
> Here's an example:
>
> 1. I have some service POJO class, annotated with @Creatable
> 2. I want to inject this service into an
> org.eclipse.core.runtime.jobs.Job, using the @Inject annotation
>
> When i only use these 2 annotations, no service is being injected, and
> the service is null at runtime.
>
> However, when i implicitly call "ContextInjectionFactory.inject(this,
> context);" inside the constructor of the Job class, the service is being
> injected.
>
> Is this the regular way to do this in eclipse plugins? I would have
> assumed that the Job subclasses are managed by the eclipse context,
> hence i don't need to manually inject the dependencies.
>
> Any info on that would be greatly appreciated.
>
>
Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1033403 is a reply to message #1033388] Thu, 04 April 2013 07:47 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1339
Registered: July 2012
Senior Member
Well, how are you creating the Job? If you are creating the Job with "new", than how should the framework know that something needs to be injected?

You could also create your Job by using "ContextInjectionFactory.make()". Then the injection should happen.
Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1033478 is a reply to message #1033388] Thu, 04 April 2013 09:11 Go to previous messageGo to next message
Robert Gruendler is currently offline Robert Gruendler
Messages: 66
Registered: June 2011
Member
thanks for your tips, makes totally sense.

So just to make sure: For regular eclipse plugins, the only thing to make DI work is to use the ContextInjectionFactory API properly, like you mentioned above.

No need for any additional configuration in the plugin.xml or somewhere else?

Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1033883 is a reply to message #1033388] Thu, 04 April 2013 19:00 Go to previous messageGo to next message
Robert Gruendler is currently offline Robert Gruendler
Messages: 66
Registered: June 2011
Member
Just had a case where i'm not creating an instance of a class myself using "new", but the class is created by eclipse, namely a PreferencePage.

The PreferencePage is defined in my plugin.xml, and using @Inject for the class attributes results in a null value for the property instead of the dependency being injected.

Any idea how to handle this case properly?


Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1033946 is a reply to message #1033883] Thu, 04 April 2013 21:08 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5419
Registered: July 2009
Senior Member
Old stuff is not created through DI, plugin.xml stuff is created through
the Equinox-Extension-Registry which does NOT even know about our DI
framework.

Tom

On 04.04.13 21:00, Robert Gruendler wrote:
> Just had a case where i'm not creating an instance of a class myself
> using "new", but the class is created by eclipse, namely a PreferencePage.
> The PreferencePage is defined in my plugin.xml, and using @Inject for
> the class attributes results in a null value for the property instead of
> the dependency being injected.
>
> Any idea how to handle this case properly?
>
>
>
Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1034274 is a reply to message #1033388] Fri, 05 April 2013 08:28 Go to previous messageGo to next message
Robert Gruendler is currently offline Robert Gruendler
Messages: 66
Registered: June 2011
Member
i see. But when you refer to "old stuff": This is what you get when you create a new plugin project in PDT (Kepler).

Does this mean creating a new plugin project in Kepler results in starting with a legacy plugin? How does plugin development work with the "new stuff" ??

This is pretty confusing. Most of the resources out there only talk about eclipse 4 in regard to RCP development, and starting a new plugin project in Kepler apparently results in a plugin skeleton wich uses legacy code.

Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1034282 is a reply to message #1034274] Fri, 05 April 2013 08:39 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5419
Registered: July 2009
Senior Member
It really depends on what you want to develop. What is the new plugin
you are trying to provide? An editor, a view?

The pure e4 stuff currently lacks e.g. informations needed to define
editors so you simply can't define an editor in pure e4 sense. So IMHO
your best bet if you want to use the new DI stuff is that you use the so
called e4 bridge (simply search for it in google).

It brings the DI stuff to your 3.x development story. There's bug 376486
which request allowing to contribute to the IDE like you contribute to
an RCP app but this is planned for Luna only (to some extend you can use
already today and there are examples like the live-model-editor).

Tom

On 05.04.13 10:28, Robert Gruendler wrote:
> i see. But when you refer to "old stuff": This is what you get when you
> create a new plugin project in PDT (Kepler).
>
> Does this mean creating a new plugin project in Kepler results in
> starting with a legacy plugin? How does plugin development work with
> the "new stuff" ??
>
> This is pretty confusing. Most of the resources out there only talk
> about eclipse 4 in regard to RCP development, and starting a new plugin
> project in Kepler apparently results in a plugin skeleton wich uses
> legacy code.
>
>
Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1034473 is a reply to message #1033388] Fri, 05 April 2013 13:23 Go to previous messageGo to next message
Robert Gruendler is currently offline Robert Gruendler
Messages: 66
Registered: June 2011
Member
great, thanks a lot for your tips!
Re: Dependency Injection in eclipse plugins (not RCP apps) [message #1062123 is a reply to message #1034473] Thu, 06 June 2013 06:41 Go to previous message
sumit singh is currently offline sumit singh
Messages: 131
Registered: October 2012
Location: Bangalore
Senior Member

Thanks for great Answer. I also had same problem.
Previous Topic:EclipseContextFactory Inject my own classes
Next Topic:Key binding for the classic Ctrl+A on tables in native e4
Goto Forum:
  


Current Time: Thu Oct 23 17:18:44 GMT 2014

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

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