Search code examples
eclipsedependency-injectioneclipse-rcpe4

Is it Possible to use the Dependency Injection (DI) in 3.x base Eclipse code imported on Luna (Eclipse e4)


Currently I'm trying to migrate my Eclipse 3.x code to e4 and I would like to access the e4 Dependency Injection (DI) feature. I'm not doing a complete migration as described here. Just I'm importing the 3.x sources on top of e4 to gain access the DI feature for Business logics (and not for UI).

I observed that the DI is not working if I follow the this approach. Any ways to acheive DI in Eclipse 3.x sources imported on Luna ?


Solution

  • If an object is just created using new xxxx then it is not injected.

    Objects created by Eclipse from a description in the Application.e4xmi (and fragments) are injected automatically (so handlers, parts, ....). @Creatable objects created by the injector are also injected.

    You can inject other objects using ContextInjectionFactory, this requires that you have access to the IEclipseContext.

    MyClass myClass = ContextInjectionFactory.make(MyClass.class, eclipseContext);
    

    creates a new instance of a class with injection.

    MyClass myClass = new MyClass();
    
    ContextInjectionFactory.inject(myClass, eclipseContex);
    

    Injects an object after it has been created using new. In this case the class Constructor cannot use injection.

    Note: There are multiple Eclipse Contexts in a tree structure. A part has its own context, so if you create the object using that context the object will only be available in the part (and any handlers etc. associated with the part).

    To make an object available everywhere use the Eclipse Context from the MApplication (call the getContext() method).