Eclipse 4 is now a full dependency injection container with @Creatable

If you want to get your own Java objects injected into a model elements just annotated them with @Creatable. If you then tell Eclipse that you need an instance of the your class it will generate it for you.

For example, lets say you have the following domain model.

@Creatable
class Todo {
 @Inject
 public Todo(Dependent depend, YourOSGiService service) {
     // placeholder
 }
}

@Creatable
class Dependent {
 public Dependent() {
 // placeholder
 }
}

Assuming that you have defined the required OSGi service, you can get an instance of your Todo data model injected in a Part.

// Field Injection
@Inject Todo todo

Pretty cool, I think. For details check out the Bug report.

To use this please use one of the recent integration builds. Eclipse 4.2M5 does not yet contain the feature.

I added this also to Eclipse 4 Tutorial. It is also included in my Eclipse 4 RCP training.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse and tagged , . Bookmark the permalink.

6 Responses to Eclipse 4 is now a full dependency injection container with @Creatable

  1. sven says:

    Not like that
    @Creatable
    class Todo {
    @Inject
    public Todo(Dependent depend, YourOSGiService service) {
    // placeholder
    }
    }

    @Creatable
    class Dependent {
    public Dependent() {
    // placeholder
    }
    }

  2. Lars Vogel says:

    @Sven: that should also work, the second @Inject in my example is redundant.

  3. sven says:

    ok, but i think

    … public Dependent1(Dependent depend, YourOSGiService service) { …

    has to be

    … public Todo(Dependent depend, YourOSGiService service) { …

    because in class Todo there could not be a contructor of Dependent1 (and @Inject is imho only for the constructor parameters) or am i wrong?

  4. Lars Vogel says:

    @Sven: Ups, yes. Corrrected my example. Thanks!

  5. What does this do?

  6. Lars Vogel says:

    @Neil: If you annotate a POJO with his annotation the Eclipse 4 DI container will automatically generate an instance of this class if this class cannot not be found in the Eclipse Context.

    Does this answer you question?

Comments are closed.