javadependency-injectioneclipse-rcpe4efxclipse

Java class annotated with @Creatable is missing attributes when injected on other plugin class


I have a e4 application project which consists of following projects

app

app.feature

app.product

app.releng

then 2 plugin projects

app.service

app.ui

I have created a simple login dialog page on a handler, I inject the service on this Dialog and then it successfully authenticates on the server.

@Creatable
public class AuthenticationService {

    @Inject
    public AuthenticationService() {

    }

    private Token token;

    public Token getToken() {
        return token;
    }

    private void setToken(Token token) {
        this.token = token;
    }

    public Token authenticate(String username, String password) {


    //authenticate and set token here
    }
}

The issue is that when I inject the same authentication service on a Part class, the retrieved token is null. i need it on a Part class since i will call another REST service to have a list of items to be displayed.

the Dialog and Part class reside on app.ui plugin project, while the authentication service is on app.service plugin project


Solution

  • If you just use @Creatable a new instance of the class is created each time you inject it. Here you want there to be only one instance of the service class so that you get the same one every time.

    To do this specify the @Singleton annotation:

    @Creatable
    @Singleton
    public class AuthenticationService {