I have two beans using @Named
, one with @SessionScoped
and the other one with @ViewScoped
. I can inject the @ViewScoped
bean into the @SessionScoped
and I try to do the opposite, and I almost works, but I don't have the same instances.
I can see it when I print this.hashCode()
inside the @PostContruct
method of the view scoped bean and compare it to the one injected inside the session scoped bean.
So I found a solution but I don't know if it is a good practice: inside the @PostContruct
method of the view scoped bean after the session scoped bean has been injected, I send the view scoped into the session scoped through a setter.
If I have well understood these objects are tied to the user so it doesn't make any trouble, am I right?
@Named
@ViewScoped
public class ViewScopedBean {
@Inject
protected SessionScopedBean sessionScopedBean;
@PostContruct
public void init() {
sessionScopedBean.setViewScopedBean(this);
}
}
@Named
@SessionScoped
public class SessionScopedBean {
protected ViewScopedBean viewScopedBean;
public void setViewScopedBean(ViewScopedBean viewScopedBean) {
this.viewScopedBean = viewScopedBean;
}
}