Reputation: 29
I am trying to inject my components and beans using Vaadin-cdi. Note the code below is simplified a bit.
@Theme("valo")
@CDIUI("")
public class MyUI extends UI {
@Inject
private CDIViewProvider provider;
@Override
protected void init(VaadinRequest request) {
Navigator navigator = new Navigator();
navigator.addProvider(provider);
navigator.navigateTo("mypanel");
}
}
And here goes MyPanel:
@CDIView("mypanel")
public class MyPanel extends com.vaadin.ui.Panel implements View {
@Inject
private MySubPanel mySubPanel;
@Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
FormLayout layout = new FormLayout();
layout.addComponent(mySubPanel);
this.setContent(layout);
}
}
And here is the MySubPanel class:
@UIScoped
public class MySubPanel extends Panel {
@Inject
private MyBean myBean;
public MySubPanel() {
myBean.createSomething("Something"); // throws NullPointerException
}
}
And finally the MyBean class:
@Stateless
@Default
public class MyBean implements Serializable {
private String something;
public void createSomething(String something) {
this.something = something;
}
}
So why does not my bean in MySubPanel
beeing injected?
I have an empty beans.xml
and I am using WildFly 8.1.
Upvotes: 2
Views: 764
Reputation: 2456
In the constructor class variables are not yet injected. The bean will be injected after initialising, so it is null in the constructor. You have 2 possibilities.
I'd recommend the second approach. Here you will find more about the different injection methods: http://www.javacodegeeks.com/2013/05/java-ee-cdi-dependency-injection-inject-tutorial.html
Upvotes: 2