How to use eclipse 4 DI in classes that are not attached to the application model?
Asked Answered
A

2

9

I have created a OSGI service with declarative services to inject an object that implements an interface. If I inject the object in a class that is attached to the application model (handler,part,....) it is working fine. If I inject it in a class that is not attached to the application model it is always returning null.

Is it possible to use DI in classes that are not attached to the application model? I looked in the vogella tutorials but somehow I don't find a solution.

Alded answered 24/12, 2012 at 7:5 Comment(0)
D
6

I know of three ways of how Eclipse 4 can inject objects in your classes:

  1. During start-up the Eclipse runtime looks for relevant annotations in the classes it instantiates.
  2. Objects injected in 1. are tracked and will be re-injected if changed.
  3. Manually triggering injection using the ContextInjectionFactory and IEclipseContext.

What you want may be possible with the third option. Here is a code example:

    ManipulateModelhandler man = new ManipulateModelhandler();

    //inject the context into an object
    //IEclipseContext iEclipseContext was injected into this class
    ContextInjectionFactory.inject(man,iEclipseContext);

    man.execute();

The problem is, however; that the IEclipseContext already needs to be injected into a class that can access the object that needs injection. Depending on the number of necessary injections, it might be more useful to use delegation instead (testability would be one argument).

    @Inject
    public void setFoo(Foo foo) {
        //Bar is not attached to the e4 Application Model
        bar.setFoo(foo);
    }

Therefore, a better solution is probably using the @Creatable annotation. Simply annotate your class, and give it a no-argument constructor.

   @Creatable
   public class Foo {
       public Foo () {}
   }

Using @Inject on that type as in the method above, will let Eclipse instantiate and inject it. The disadvantage is that you cannot control the object creation anymore, as you would with ContextInjectionFactory.inject(..).

Davedaveda answered 25/12, 2012 at 1:22 Comment(3)
Just a small note that most people might want to look at injecting a javax.inject.Provider<T> into the containing class which requires the injected instances, as an alternative to 3.Eryneryngo
How can I get the IEclipseContext if the statement ContextInjectionFactory.inject(man,iEclipseContext) running in a class which is not defined in Application Model?Espinosa
@Espinosa Probably this answer will help you. But it is important to point out that this won't give you the context, but rather a context. Eclipse keeps a tree of context objects, and whether or not one of them contains the values you need, depends on your use case. Maybe have a look at E4Application.createDefaultContext() as well.Davedaveda
O
0

I refactored out some part of e(fx)clipse in order to achieve that. Have a look at this. Sorry for the shameless plug...

Obloquy answered 8/1, 2015 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.