JSF initialization fails with @ViewScoped

302 views Asked by At

I am building a JSF 2.3 application with Primefaces 8.0 on MyFaces 2.3.6, running on Tomcat 9. Also using OpenWebBeans 2.0.18.

One of my beans (controller) gives me an error when I change the scope from @RequestScoped to @ViewScoped:

28-Sep-2020 13:12:07.368 SEVERE [main] org.apache.myfaces.webapp.AbstractFacesInitializer.initFaces An error occured while initializing MyFaces: It's not allowed to call getReference(Bean, Type, CreationalContext) before AfterDeploymentValidation
java.lang.IllegalStateException: It's not allowed to call getReference(Bean, Type, CreationalContext) before AfterDeploymentValidation
    at org.apache.webbeans.container.InjectableBeanManager.checkAfterDeploymentValidationFired(InjectableBeanManager.java:434)
    at org.apache.webbeans.container.InjectableBeanManager.getReference(InjectableBeanManager.java:178)
    at org.apache.myfaces.cdi.util.CDIUtils.resolveInstance(CDIUtils.java:65)
    at org.apache.myfaces.cdi.util.CDIUtils.lookup(CDIUtils.java:52)
    at org.apache.myfaces.flow.cdi.DefaultCDIFacesFlowProvider.getAnnotatedFlows(DefaultCDIFacesFlowProvider.java:52)
    at org.apache.myfaces.flow.impl.AnnotatedFlowConfigurator.configureAnnotatedFlows(AnnotatedFlowConfigurator.java:42)
    at org.apache.myfaces.config.FacesConfigurator.configureFlowHandler(FacesConfigurator.java:1713)
    at org.apache.myfaces.config.FacesConfigurator.configure(FacesConfigurator.java:619)
    at org.apache.myfaces.webapp.AbstractFacesInitializer.buildConfiguration(AbstractFacesInitializer.java:456)
    at org.apache.myfaces.webapp.Jsp21FacesInitializer.initContainerIntegration(Jsp21FacesInitializer.java:70)
    at org.apache.myfaces.webapp.AbstractFacesInitializer.initFaces(AbstractFacesInitializer.java:190)
    at org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:103)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4685)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5146)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:633)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:344)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:475)

The bean code is along these lines:

@Named
@ViewScoped
@ManagedBean
public class DashBoardController implements Serializable
{
    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(DashBoardController.class.getName());

    List<IdentityStatistics> idStats;
    List<AuditStatistics> auditStats;

    @Inject
    private StatisticsService service;

    @PostConstruct
    public void init()
    {
        logger.log(Level.FINE, "Reading statistics from API");
        FacesContext context = FacesContext.getCurrentInstance();
        AuthToken token =    (AuthToken)context.getExternalContext().getSessionMap().get(AuthService.SESSION_TOKEN);
        ...
    }
    ...
}

Switching back to @RequestScoped immediately resolves the error (but that is not what I want). The getReference() method that is mentioned in the error is definitely not in my code, so it must be in a framework library somewhere.

I have another @ViewScoped bean that is quite similar, which does not give me this error. Apparently there is a difference somewhere that I am missing, but for the life of me I cannot spot it.

What could cause this error when using @ViewScoped ?

1

There are 1 answers

3
mvreijn On

OK. The simple answer is: the StatisticsService service that the failing bean contains using @Inject did not implement Serializable.

In request scope that is not a problem, obviously in other scopes the state needs to be preserved.

It would have been nice if the error was a bit less cryptic.