Field level injection with Gin

I'm trying to do field-level injection so I don't have to pass "models" when my controllers are instantiated, like,

UserController controller = new UserController(/*No need to pass models here*/);

However my application throws NullPointerException, here my code:

UserController.java

    public class UserController implements Controller {
        @Inject private UserModel model;
        public UserController() {
          model.doSomething(); // NullPointerException
        }
    }

ClientGinModule.java

public class ClientGinModule extends AbstractGinModule {
    @Override
    protected void configure() {
        bind(UserModel.class).in(Singleton.class);
    }
}

What could be the problem?

Answers


Use In Guice

UserController controller = injector.getInstance(UserController.class);

Use in Gin:

// Declare a method returning a UserController on your interface extending Ginjector
public UserController getUserController();

// When you need the controller, just call:
injector.getUserController();

to get a fully-injected controller.


Your model field will be null while constructor is still running. It will be injected by GIN after the moment when UserController object is fully created. Here GWT GIN Field Level Injection you can find nice explanation about it.


Need Your Help

Windows WF 3.5 How to drop activity on Custom Composite Activity in Workflow

c# workflow-foundation

I have a custom activity which is a composite of several activities. There are some activities on the composite where I would like users of the custom activity to be able to drop more activities at

Modal view does not fire from child controller

ios objective-c viewcontroller

My MainViewController loads another view modally.