Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 766
Yves_T Avatar asked Dec 24 '12 07:12

Yves_T


People also ask

What is dependency injection in eclipse?

The Eclipse 4 Application Platform has adopted the use of Dependency Injection (DI) to circumvent these problems: rather than require client code to know how to access a service, the client instead describes the service required, and the platform is responsible for configuring the object with an appropriate service.

What is dependency injection in java?

Dependency Injection in Java is a way to achieve Inversion of control (IoC) in our application by moving objects binding from compile time to runtime. We can achieve IoC through Factory Pattern, Template Method Design Pattern, Strategy Pattern and Service Locator pattern too.

Why is dependency injection useful?

Dependency injection can be useful when working with large applications because it relieves various code modules from the task of instantiating references to resources and allows dependencies -- even mock dependencies -- to be swapped out easily, which can make unit testing easier.

What is Eclipse e4?

e4 is an incubator for community exploration of future technologies for the Eclipse Platform. The project has a number of principal aims: Explore technologies for inclusion in the SDK: This will allow us to explore and incubate new technologies and patterns for use in the SDK.


1 Answers

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(..).

like image 151
Max Hohenegger Avatar answered Oct 05 '22 00:10

Max Hohenegger