Just move on to JSF 2.2 and CDI 1.2. Injection will be at least easier. Saving according to @BalusC original answer:
import javax.enterprise.context.RequestScoped; import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.inject.Named; @Named @RequestScoped public class RequestBean { @Inject private SessionBean sessionBean;
from
import java.io.Serializable; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; @Named @SessionScoped public class SessionBean implements Serializable { private SomeObject someVariable;
There are several important @Named , in particular, switching to @Named and the full package name for RequestScoped and SessionScoped . Also, to create the SessionScoped class SessionScoped it must also be made Serializable .
Adding @Inject makes it very simple - but sessionBean that the sessionBean object you sessionBean is only available after building, not at time. This means that you do not have access to sessionBean inside the RequestBean constructor. This is solved using @PostConstruct , which starts after the injection is completed, and the RequestBean is otherwise fully initialized.
YoYo Apr 10 '17 at 2:27 2017-04-10 02:27
source share