I have a Managed Bean:
public class CategoriaManagedBean {
@EJB
private CategoriaBeanLocal categoriaBean;
private Categoria categoria;
private List<Categoria> menu;
}
In my constructor I try:
public CategoriaManagedBean() {
menu = categoriaBean.findByIdCategoriaPadre(0);
}
But I get a error "Cannot create the instance of the class", why can't I initialize the attribute in the constructor?
I fix the problem with:
@PostConstruct
public void init() {
menu = categoriaBean.findByIdCategoriaPadre(0);
}
But I want to know the reason and if I am doing well with @PostConstruct
Greetings.
Using @PostConstruct
is the correct approach.
EJBs are injected after the constructor is invoked on a ManagedBean.
That's why there is a @PostConstruct
annotation.
Here's the first line from the documentation:
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.