I try to build a Webapp with the MVP paradigm. Because I want the API to be clean and make everything easy testable I try to inject everything possible via Contructor Inject
I got it to work now. The hint from Nikita Beloglazov helped a lot because as I understand it the problem really is the moment when the instantiation is done. As I want to stay with the constructor injection I chose the second approach:
public Class PresenterImpl implements Presenter {
private ViewImpl view;
private Instance instanceView;
private User user;
@Inject
public PresenterImpl(Instance instanceView, User user) {
this.instanceView = instanceView;
this.user = user;
bind();
}
public void bind() {
this.view = instanceView.get();
}
public void fetchNames() {
fetchFromDB();
view.setUser(user);
}
}
To get this to work I have to inject only a proxy object at the time of constructor injection and get the real instance at the moment when the first action on the object is performed. This is the normal behavior of normal scoped beans. So I made the presenter and the view @SessionScoped and extended the interfaces from Serializable. Now the Constructor injection works and the ViewImpl is "lazy injected".