Injecting own object to global Eclipse context [message #1618928] |
Mon, 16 February 2015 08:11 |
|
Dear experts,
I am currently injecting my own object to the global Eclipse context, so to make it available through injection anywhere. I am doing this in the plugin activator.
As far as I understand, I can achieve injection of an already created myClass instance (myObj) either by IEclipseContext.set or via CIF:
@Inject
IEclipseContext eclipseContext;
[...]
MyClass myObj = new MyClass(a, b, c, d, e, f, bugsBunny, omega) ;
ContextInjectionFactory.inject(myObj, eclipseContext); // A
eclipseContext.set(MyClass.class, myObj); // B
After this, when application model objects get loaded, I want to exploit DI to get my myObj injected but I cannot get this to work. In both cases I get:
@Inject
MyClass myObj;
||
||
\/
org.eclipse.e4.core.di.InjectionException: Unable to process "Foo.myObj": no actual value was found for the argument "MyClass".
What am I doing wrong?
thanks a lot for your patience,
-Piero
[Updated on: Mon, 16 February 2015 08:41] Report message to a moderator
|
|
|
Re: Injecting own object to global Eclipse context [message #1618934 is a reply to message #1618928] |
Mon, 16 February 2015 08:18 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
How do you get access to the context in the Activator? Generally
speaking you can't get access to the correct root context very easily
inside your Activator.
Tom
On 16.02.15 09:11, Piero Campalani wrote:
> Dear experts,
>
> I am currently injecting my own object to the global Eclipse context, so
> to make it available through injection anywhere. I am doing this in the
> plugin activator.
>
> As far as I understand, I can achieve injection of an already created
> myClass instance (myObj) either by IEclipseContext.set or via CIF:
> @Inject
> IEclipseContext eclipseContext;
> [...]
> MyClass myObj = new myClass(a, b, c, d, e, f, bugsBunny, omega) ;
> ContextInjectionFactory.inject(myObj, eclipseContext); // A
> eclipseContext.set(myClass.class, myObj); // B
>
> After this, when application model objects get loaded, I want to exploit
> DI to get my myObj injected but I cannot get this to work. In both cases
> I get:
>
>
> @Inject
> MyClass myObj;
> ||
> ||
> \/
> org.eclipse.e4.core.di.InjectionException: Unable to process
> "Foo.myClass": no actual value was found for the argument "myClass".
>
> What am I doing wrong?
>
> thanks a lot for your patience,
> -Piero
|
|
|
Re: Injecting own object to global Eclipse context [message #1618960 is a reply to message #1618934] |
Mon, 16 February 2015 08:37 |
|
Thanks Tom.
I am using the EclipseContextFactory to get it (or at least to get an instance of that, which at this point I think might get replaced by a new context after Activator's call):
IEclipseContext eclipseContext = EclipseContextFactory
.getServiceContext(bundleContext);
If I use the method suggested in Eclipse 4 RCP Development How-to by Ram Kulkarni (2013) then (I see now): everything is fine.
IEclipseContext eclipseContext = E4Workbench.getServiceContext();
I opted for what I thought was a less hacky method (ECF) because E4Worlbench is warned to be not API (yet?).
[Updated on: Mon, 16 February 2015 08:38] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: Injecting own object to global Eclipse context [message #1622327 is a reply to message #1619228] |
Wed, 18 February 2015 12:17 |
|
Just for the sake of completeness..
I successfully injected my own object in the Eclipse application context by using a context function.
I followed the tutorial by Lars on the matter: link.
As suggested there, if you persist the object in the application context, then the context function will just be called once.
@Lars Vogel: I had to find a separate article (this one) in order to understand a proper location of the component definition (OSGI-INF/) and how to register my component in the plugin MANIFEST (using Service-Component + lazy): you might want to complete the information in your tutorial.
[Updated on: Wed, 18 February 2015 12:42] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.02422 seconds