Inconsistent @Inject of class members

57 views
Skip to first unread message

Thad Humphries

unread,
Mar 29, 2014, 8:59:11 PM3/29/14
to googl...@googlegroups.com
I'm using GIN to create MVP activities and their views. In some activities, I am able to inject private members--interfaces for GWT.create() and POJO singletons--while in other activities these same object type members are null in my start() method.

Below the member Messsages is an interface that extends com.google.gwt.i18n.client.Messages (a GWT.create()-able object), while the view is injected from my GIN module using `bind(MotdView.class).to(MotdViewGwtImpl.class).in(Singleton.class);`. This injection works:

  @Inject private Messages messages;
  ...
  @Inject
  public MotdActivity(MotdView view) {
    super(view, "nav");
    this.view = view;
  }
  
Below BeanFactory is an interface that extends com.google.web.bindery.autobean.shared.AutoBeanFactory (a GWT.create()-able object) while AppState is a Singleton POJO. This injection works (the beanFactory can decode text from the server into an AutoBean):

  @Inject private QueriesView view;
  @Inject private BeanFactory beanFactory;
  @Inject private AppState appState;
  ...
  @Inject
  public QueriesActivity() {}


However in the member injections below do NOT work--BeanFactory and AppState are null in my start() method:

  @Inject private BeanFactory beanFactory;
  @Inject private AppState appState;
  ...
  @Inject
  public QueryFormActivity(QueryFormView view, QueryFormPlace place) {
    super(view, "nav");
    this.view = view;
    this.place = place;
    ...
    
For QueryFormActivity, the view and place are provided by my ActivityMapper:

  if (place instanceof QueryFormPlace) {
    return new QueryFormActivity(injector.getQueryFormView(),
      (QueryFormPlace)place);
  }

I can modify QueryFormActivity's new method and also pass the BeanFactory and AppState using the injector--that works--but why must I? Why aren't they injected as they were in QueriesActivity? Could the member injection be failing because place is not injected? Is there some rule set for when member inject doesn't work.


Jens

unread,
Mar 30, 2014, 10:54:16 AM3/30/14
to googl...@googlegroups.com
If you create an object yourself, GIN does not know about it and thus any injection for that class will not work anymore. By using "new" you are basically breaking the injection chain.

In your example you need to inject QueryFormActivity into your mapper. To do make that work you would either need to introduce a method QueryFormActivity.setPlace(Place) or you have to use assisted inject so you can create QueryFormActivity with some constructor parameters injected (the view) and some provided by yourself (the place). In case of assisted inject you would not inject the activity directly into your mapper but instead inject a factory that can create your activities, see:


-- J.

Thad Humphries

unread,
Mar 30, 2014, 4:39:50 PM3/30/14
to googl...@googlegroups.com
Thank you. I understand that logic. I've looked a wee bit at assisted injection. I'll look deeper.

Christopher Gammage

unread,
May 8, 2014, 5:41:18 PM5/8/14
to googl...@googlegroups.com
Doesn't GIN offer a the ability to do injection on an object created outside of GIN (like with new).    For example by creating a method "void injectMember(QueryFormActivity queryFormActivity)" in your injector and then calling it from your injector instance?


Jens

unread,
May 8, 2014, 7:11:33 PM5/8/14
to googl...@googlegroups.com
Doesn't GIN offer a the ability to do injection on an object created outside of GIN (like with new).    For example by creating a method "void injectMember(QueryFormActivity queryFormActivity)" in your injector and then calling it from your injector instance?

Hm cool, didn't know that. But as I strongly prefer constructor injection, thats probably no wonder.

-- J.
Reply all
Reply to author
Forward
0 new messages