CDI injection into Views not working?

I’m having a strange issue with vaadin-cdi, I’m getting injections into my CDIUI bean but not into my views. I have injected the CDIViewProvider and set it as the navigator-provider and when I navigate, I see that it resolves my View beans but all the injections are null. How can the injections be null when they come from the BeanManager?

First thing to check: are you trying to use the injected fields in the constructor (or something called from the constructor)? If that is the case, try if moving the code to a @PostConstruct method or the attach() method helps (making sure to also call super.attach() in the latter case).

If that is not the problem, check the scopes of the injected beans.

Moro Henri :wink:

I’m doing a

@Inject
private LoginService loginService

where LoginServiceImpl implements LoginService, essentially making it CDI normal scoped

when I inject in my @CDIUI(“”) UI class I see the injection just fine but when I get moved along to my
@CDIView(“”) LoginView extends CustomComponent implements View class, the same injection is null.

The only reasonable explanation I could see for this is if the CDIViewProvider would instantiate the views with normal constructors instead of getting them from the BeanManager. Do I need any extra markers/annotations for this to work?

Thanks in advance,
Nik

Actually it is apparently even more confusing, the injections are done in my LoginView but upon successful login I do

getUI().getNavigator().navigateTo(“main”);

and there the injections are null. I debugged the lookup and it looks fine but for some reson the injections are not done. So to summarize: I have two services LoginService and OrderService with normal-scoped implementations. I have two views, LoginView and MainView. I get LoginService injections into my root UI and into the LoginView but when I move on from LoginView to MainView, the OrderService is null…

And since we don’t want this to be too trivial, if I inject a test class

public class Test
{
@Inject
private MainView mv;

public void test()
{
mv.enter(null);
}
}

Then my MainView has the OrderService injected.

So. What does the CDI view resolver do so it manages to get an instance of the MainView without injections done?

This sounds quite strange. CDIViewProvider is using the BeanManager to resolve the views:
CDIViewProvider
. The
tutorial application
also does injections to views.

Assuming that the MainView does come from CDIViewProvider and isn’t registered in some other way, I’d still suspect some scope issue between the MainView and the service you try to inject, but it is hard to say for certain. As it has been quite a while since I last touched CDI and Vaadin CDI, I don’t have the exact scoping details in my head.

MainView does come from the CDIViewProvider (debugged it there) and if I in the MainView constructor do a manual lookup of the service through BeanManager, it works just fine.

Hmm. Working now - I must have been trying too many different things at the same time and got myself mixed up.