3

Now with the @Creatable annotation is possible to mark a class to be injected without adding it in the EclipseContext by hand in the life cycle:

http://blog.vogella.com/2012/02/29/eclipse-4-is-now-a-full-dependency-injection-container-with-creatable/

However, what about the following scenario: lets say that I have an interface SomethingService and some number of implementations, and I want to refer to one of those (i.e. the one annotated as creatable) by its interface, something like:

@Creatable
class Todo implements SomethingService {
    @Inject
    public Todo(SomeArg arg) {
    // placeholder
    }
 }

 // Field Injection
 @Inject private SomethingService service;  // Todo instance 

This doesn’t seem to work at all. Is there a way to achieve what I need?

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
mreparaz
  • 129
  • 6
  • Do you have `SomeArg` in the Eclipse context so that it can be found? Eclipse only does injection on objects that the application model knows about (parts, handlers...). – greg-449 May 21 '14 at 07:55
  • Yes, it is. It was just an example, if I use an empty constructor the result is the same. – mreparaz May 21 '14 at 15:11
  • What class are you trying to inject SomethingService in to, as I said this must be something that the application model knows about - otherwise no injection is done and you get null. – greg-449 May 21 '14 at 15:21
  • Yes, I'm injecting the SometingService into a Part from the Application.e4xmi In fact, another things are injected correctly, like Declarative Services or the Event Broker. And if I change the "SomethingService service" to "Todo service" it works. – mreparaz May 21 '14 at 20:17

1 Answers1

4

Well, from what I could see what I'm asking should be done with the OSGi Declarative Services or like the example from "Eclipse 4 Plug-in Development by Example" modifying the Activator like this:

public class Activator implements BundleActivator {
    public void start(BundleContext bundleContext) throws 
        InjectorFactory.getDefault().
           addBinding(IStringService.class).implementedBy(StringService.class);
    }
}

Thanks

mreparaz
  • 129
  • 6