How to inject a "runtime" dependency like a logged in user which is not available at application boot time?

Posted by Fabian on Stack Overflow See other posts from Stack Overflow or by Fabian
Published on 2011-01-07T20:06:36Z Indexed on 2011/01/12 0:53 UTC
Read the original article Hit count: 240

Filed under:
|
|
|
|

I'm just not getting this:

I use Gin in my java GWT app to do DI. The login screen is integrated into the full application window. After the user has logged in I want to inject the user object into other classes like GUI Presenters which I create, so I have some sort of runtime dependency I believe. How do i do that?

One solution I can think of is sth like:

class Presenter {
  @Inject
  Presenter(LoggedInUserFactory userFactory) {
     User user = userFactory.getLoggedInUser();
  }
}

class LoggedInUserFactoryImpl {
  public static User user;
  User getLoggedInUser() {
    return user;
  }
}

So, when the user is successfully logged in and I have the object i set the static property in LoggedInUserFactory, but this will only work if the Presenter is created after the user has logged in which is not the case.

Or should I use a global static registry? I just don't like the idea of having static dependencies in my classes.

Any input is greatly appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about gwt