Search code examples
javaeclipsedependency-injectioneclipse-rcprcp

Not able to use @Inject in eclipse 4


I am trying to use dependency injection in my eclipse project. I included maven dependency for org.eclipse.e4.core.di in my dependency management pom.

<dependency>
    <groupId>org.eclipse.e4</groupId>
    <artifactId>org.eclipse.e4.core.di</artifactId>
    <version>1.5.0-SDK-4.5.0</version>
</dependency>

And also, in the plugin where I am using dependency injection with compile scope.

<dependency>
    <groupId>org.eclipse.e4</groupId>
    <artifactId>org.eclipse.e4.core.di</artifactId>
    <version>compile</version>
</dependency>

After this, when I try to use the annotation @Inject, I am seeing error and I am not able to see any import suggestion also.

I have tried including the following in the pom also

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.2</version>
</dependency>

What is causing this problem? I am using Java 8.


Solution

  • To use @Inject you must include javax.inject in the Require-Bundle or Import-Package entries in the MANIFEST.MF of your plug-in.

    To do this in the MANIFEST.MF editor go to the Dependencies tab and add javax.inject to the Required Plug-ins or Imported Packages section.

    When you have done this you should be able to use

    import javax.inject.Inject;
    

    in your code.

    Note: Only classes created from entries in the e4 Application.e4xmi are injected unless you use ContextInjectionFactory to create the class.